We use a python dictionary to store key-value pairs. Similarly, Dataframes are used to store records containing values associated with a key in the tabular format. In this article, we will discuss how we can append a dictionary to a dataframe in Python.
How to Append a Dictionary to a Dataframe in Python?
To append a dictionary to a pandas dataframe, we will use the append()
method. The append()
method, when invoked on a dataframe, takes a dictionary as its input argument and returns a new dataframe. The append()
method also takes the value True
as an input argument for its ignore_index
parameter.
The output dataframe contains the dictionary values appended as a row in the original dataframe. You can observe this in the following example.
import pandas as pd
names=pd.read_csv("name.csv")
print("The input dataframe is:")
print(names)
myDict={"Class":3, "Roll":22, "Name":"Sakshi"}
print("The dictionary is:")
print(myDict)
names=names.append(myDict,ignore_index=True)
print("The output dataframe is:")
print(names)
Output
The input dataframe is:
Class Roll Name
0 1 11 Aditya
1 1 12 Chris
2 1 13 Sam
3 2 1 Joel
4 2 22 Tom
5 2 44 Samantha
6 3 33 Tina
7 3 34 Amy
The dictionary is:
{'Class': 3, 'Roll': 22, 'Name': 'Sakshi'}
The output dataframe is:
Class Roll Name
0 1 11 Aditya
1 1 12 Chris
2 1 13 Sam
3 2 1 Joel
4 2 22 Tom
5 2 44 Samantha
6 3 33 Tina
7 3 34 Amy
8 3 22 Sakshi
If a dictionary has less number of keys than the columns specified in the dataframe, the remaining columns are assigned the value NaN
in the rows where the dictionary is appended. You can observe this in the following example.
import pandas as pd
names=pd.read_csv("name.csv")
print("The input dataframe is:")
print(names)
myDict={"Class":3, "Name":"Sakshi"}
print("The dictionary is:")
print(myDict)
names=names.append(myDict,ignore_index=True)
print("The output dataframe is:")
print(names)
Output:
The input dataframe is:
Class Roll Name
0 1 11 Aditya
1 1 12 Chris
2 1 13 Sam
3 2 1 Joel
4 2 22 Tom
5 2 44 Samantha
6 3 33 Tina
7 3 34 Amy
The dictionary is:
{'Class': 3, 'Name': 'Sakshi'}
The output dataframe is:
Class Roll Name
0 1 11.0 Aditya
1 1 12.0 Chris
2 1 13.0 Sam
3 2 1.0 Joel
4 2 22.0 Tom
5 2 44.0 Samantha
6 3 33.0 Tina
7 3 34.0 Amy
8 3 NaN Sakshi
If a dictionary doesn’t have the keys same as the columns in the dataframe, a new column is added to the dataframe for each key that is not present in the dataframe as the column name. While appending a dictionary with key values different from the column names, the values for the new columns in the existing rows are filled as NaN
. Similarly, the columns which are not present in the dictionary but are present in the dataframe are assigned the value NaN
is the row where the dictionary is appended. You can observe that in the following example.
import pandas as pd
names=pd.read_csv("name.csv")
print("The input dataframe is:")
print(names)
myDict={"Class":3, "Roll":22, "Name":"Sakshi","Height":160}
print("The dictionary is:")
print(myDict)
names=names.append(myDict,ignore_index=True)
print("The output dataframe is:")
print(names)
Output:
The input dataframe is:
Class Roll Name
0 1 11 Aditya
1 1 12 Chris
2 1 13 Sam
3 2 1 Joel
4 2 22 Tom
5 2 44 Samantha
6 3 33 Tina
7 3 34 Amy
The dictionary is:
{'Class': 3, 'Roll': 22, 'Name': 'Sakshi', 'Height': 160}
The output dataframe is:
Class Roll Name Height
0 1 11 Aditya NaN
1 1 12 Chris NaN
2 1 13 Sam NaN
3 2 1 Joel NaN
4 2 22 Tom NaN
5 2 44 Samantha NaN
6 3 33 Tina NaN
7 3 34 Amy NaN
8 3 22 Sakshi 160.0
To append two or more dictionaries or a list of dictionaries to the dataframe, you can use a for loop along with the append()
method. In the for loop, you can append each dictionary to the dataframe.
Suggested Reading: If you are into machine learning, you can read this article on k-prototypes clustering with numerical example. You might also like this article on data ink ratio.
The append()
method will be deprecated in near future. So, you can use the pandas concat()
method to append a dictionary to the dataframe as shown below.
import pandas as pd
names=pd.read_csv("name.csv")
print("The input dataframe is:")
print(names)
myDict={"Class":3, "Roll":22, "Name":"Sakshi","Height":160}
print("The dictionary is:")
print(myDict)
tempDf=pd.DataFrame([myDict])
names=pd.concat([names,tempDf],ignore_index=True)
print("The output dataframe is:")
print(names)
Output:
The input dataframe is:
Class Roll Name
0 1 11 Aditya
1 1 12 Chris
2 1 13 Sam
3 2 1 Joel
4 2 22 Tom
5 2 44 Samantha
6 3 33 Tina
7 3 34 Amy
The dictionary is:
{'Class': 3, 'Roll': 22, 'Name': 'Sakshi', 'Height': 160}
The output dataframe is:
Class Roll Name Height
0 1 11 Aditya NaN
1 1 12 Chris NaN
2 1 13 Sam NaN
3 2 1 Joel NaN
4 2 22 Tom NaN
5 2 44 Samantha NaN
6 3 33 Tina NaN
7 3 34 Amy NaN
8 3 22 Sakshi 160.0
Conclusion
In this article, we have discussed how to append a dictionary to a dataframe in python. To know more about python programming, you can read this article on dictionary comprehension in python. You might also like this article on list comprehension in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.