Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Append list of dictionaries to an existing Pandas DataFrame in Python
To append a list of dictionaries to an existing Pandas DataFrame, you can use the pd.concat() method. The older append() method has been deprecated since Pandas 1.4.0.
Creating the Initial DataFrame
First, let's create a DataFrame with some initial data ?
import pandas as pd
dataFrame = pd.DataFrame({
"Car": ['BMW', 'Audi', 'XUV', 'Lexus', 'Volkswagen'],
"Place": ['Delhi', 'Bangalore', 'Pune', 'Chandigarh', 'Mumbai'],
"Units": [100, 150, 50, 110, 90]
})
print("Original DataFrame:")
print(dataFrame)
Original DataFrame:
Car Place Units
0 BMW Delhi 100
1 Audi Bangalore 150
2 XUV Pune 50
3 Lexus Chandigarh 110
4 Volkswagen Mumbai 90
Creating List of Dictionaries
Now, create a list of dictionaries that we want to append ?
import pandas as pd
# List of dictionaries to append
new_data = [
{'Car': 'Mustang', 'Place': 'Hyderabad', 'Units': 60},
{'Car': 'Tesla', 'Place': 'Kerala', 'Units': 30},
{'Car': 'RollsRoyce', 'Place': 'Punjab', 'Units': 70},
{'Car': 'Bentley', 'Place': 'Gujarat', 'Units': 80}
]
print("List of dictionaries:")
print(new_data)
List of dictionaries:
[{'Car': 'Mustang', 'Place': 'Hyderabad', 'Units': 60}, {'Car': 'Tesla', 'Place': 'Kerala', 'Units': 30}, {'Car': 'RollsRoyce', 'Place': 'Punjab', 'Units': 70}, {'Car': 'Bentley', 'Place': 'Gujarat', 'Units': 80}]
Using pd.concat() Method
The recommended way to append data is using pd.concat() ?
import pandas as pd
# Original DataFrame
dataFrame = pd.DataFrame({
"Car": ['BMW', 'Audi', 'XUV', 'Lexus', 'Volkswagen'],
"Place": ['Delhi', 'Bangalore', 'Pune', 'Chandigarh', 'Mumbai'],
"Units": [100, 150, 50, 110, 90]
})
print("Original DataFrame:")
print(dataFrame)
# List of dictionaries to append
new_data = [
{'Car': 'Mustang', 'Place': 'Hyderabad', 'Units': 60},
{'Car': 'Tesla', 'Place': 'Kerala', 'Units': 30},
{'Car': 'RollsRoyce', 'Place': 'Punjab', 'Units': 70},
{'Car': 'Bentley', 'Place': 'Gujarat', 'Units': 80}
]
# Convert list of dictionaries to DataFrame
new_df = pd.DataFrame(new_data)
# Append using concat
result_df = pd.concat([dataFrame, new_df], ignore_index=True)
print("\nAfter appending:")
print(result_df)
Original DataFrame:
Car Place Units
0 BMW Delhi 100
1 Audi Bangalore 150
2 XUV Pune 50
3 Lexus Chandigarh 110
4 Volkswagen Mumbai 90
After appending:
Car Place Units
0 BMW Delhi 100
1 Audi Bangalore 150
2 XUV Pune 50
3 Lexus Chandigarh 110
4 Volkswagen Mumbai 90
5 Mustang Hyderabad 60
6 Tesla Kerala 30
7 RollsRoyce Punjab 70
8 Bentley Gujarat 80
Key Parameters
The ignore_index=True parameter resets the index in the final result, creating a continuous index sequence starting from 0.
Conclusion
Use pd.concat() to append a list of dictionaries to a DataFrame by first converting the list to a DataFrame. The ignore_index=True parameter ensures proper index sequencing in the result.
Advertisements
