Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Append list of dictionaries to an existing Pandas DataFrame in Python
Append a list of dictionaries to an existing Pandas DataFrame, use the append() method. At first, create a DataFrame −
dataFrame = pd.DataFrame(
{
"Car": ['BMW', 'Audi', 'XUV', 'Lexus', 'Volkswagen'],"Place": ['Delhi','Bangalore','Pune','Chandigarh','Mumbai'],"Units": [100, 150, 50, 110, 90]
}
)
Create list of Dictionaries −
d = [{'Car': 'Mustang', 'Place': 'Hyderabad', 'Units': 60},{'Car': 'Tesla', 'Place': 'Kerala', 'Units': 30},{'Car': 'RollsRoyce', 'Place': 'Punjab', 'Units': 70},{'Car': 'Bentley', 'Place': 'Gujarat', 'Units': 80}
]
Now, append list of Dictionaries to an already created DataFrame −
dataFrame = dataFrame.append(d, ignore_index=True, sort=False)
Example
Following is the code −
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("DataFrame ...\n",dataFrame)
# creating dictionaries
d = [{'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("\n Dictionary....\n", d)
# appending list of dictionaries to already created DataFrame
dataFrame = dataFrame.append(d, ignore_index=True, sort=False)
# Display the appended result
print("\nResult of append...\n",dataFrame)
Output
This will produce the following output −
DataFrame ...
Car Place Units
0 BMW Delhi 100
1 Audi Bangalore 150
2 XUV Pune 50
3 Lexus Chandigarh 110
4 Volkswagen Mumbai 90
Dictionary....
[{'Units': 60, 'Car': 'Mustang', 'Place': 'Hyderabad'}, {'Units': 30, 'Car': 'Tesla', 'Place': 'Kerala'}, {'Units': 70, 'Car': 'RollsRoyce', 'Place': 'Punjab'}, {'Units': 80, 'Car': 'Bentley', 'Place': 'Gujarat'}])
Result of append...
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 80Advertisements