In this tutorial, We will see different ways of Creating a pandas Dataframe from Dictionary .
[toc]
Using a Dataframe() method of pandas.
Example 1 : When we only pass a dictionary in DataFrame() method then it shows columns according to ascending order of their names .
# import pandas package as pd in this code
import pandas as pd
# make a dictionary containing students data
data = {
'Name': ['Swapnil', 'Shivangi', 'Shaurya', 'Priya'],
'Age': [23, 22, 21, 20],
'Course': ['B.tech', 'B.tech', 'B.sc', 'B.sc'],
'College': ['Geu','Geu','Gehu','Gehu']
, }
# Convert the given dictionary into pandas DataFrame
df = pd.DataFrame(data)
# print the pandas Dataframe
print("Given Dataframe :\n", df)
Output :
Given Dataframe :
Age College Course Name
0 23 Geu B.tech Swapnil
1 22 Geu B.tech Shivangi
2 21 Gehu B.sc Shaurya
3 20 Gehu B.sc Priya
Example 2 : If we want to show a DataFrame with a specific order of columns then we have to pass a columns key word arguments alog with dictionary in the DataFrame method.
# import pandas package as pd in this code
import pandas as pd
# make a dictionary containing students data
data = {
'Name': ['Swapnil', 'Shivangi', 'Shaurya', 'Priya'],
'Age': [23, 22, 21, 20],
'Course': ['B.tech', 'B.tech', 'B.sc', 'B.sc'],
'College': ['Geu','Geu','Gehu','Gehu']
, }
# Convert the given dictionary into pandas DataFrame
df = pd.DataFrame(data, columns = ['Name', 'Age', 'Course', 'College'])
# print the pandas Dataframe
print("Given Dataframe :\n", df)
Output :
Given Dataframe :
Name Age Course College
0 Swapnil 23 B.tech Geu
1 Shivangi 22 B.tech Geu
2 Shaurya 21 B.sc Gehu
3 Priya 20 B.sc Gehu
Using DataFrame.from_dict() method.
Example 1 : When we only pass a dictionary in DataFrame.from_dict() method then it shows columns according to ascending order of their names .
# import pandas package as pd in this code
import pandas as pd
# make a dictionary containing students data
data = {
'Name': ['Swapnil', 'Shivangi', 'Shaurya', 'Priya'],
'Age': [23, 22, 21, 20],
'Course': ['B.tech', 'B.tech', 'B.sc', 'B.sc'],
'College': ['Geu','Geu','Gehu','Gehu'],
}
# Convert the given dictionary into pandas DataFrame
df = pd.DataFrame.from_dict(data)
# print the pandas Dataframe
print("Given Dataframe :\n", df)
Output :
Given Dataframe :
Age College Course Name
0 23 Geu B.tech Swapnil
1 22 Geu B.tech Shivangi
2 21 Gehu B.sc Shaurya
3 20 Gehu B.sc Priya
Example 2 : If we want to create a Dataframe using dictionary in which keys is act as rows then we have to Specify orient="index" in DataFrame.from_dict() method along with dictionary.
# import pandas package as pd in this code
import pandas as pd
# make a dictionary containing students data
data = {
'Name': ['Swapnil', 'Shivangi', 'Shaurya', 'Priya'],
'Age': [23, 22, 21, 20],
'Course': ['B.tech', 'B.tech', 'B.sc', 'B.sc'],
'College': ['Geu','Geu','Gehu','Gehu'],
}
# Convert the given dictionary into pandas DataFrame
df = pd.DataFrame.from_dict(data, orient = "index")
# print the pandas Dataframe
print("Given Dataframe :\n", df)
Output :
Given Dataframe :
0 1 2 3
Name Swapnil Shivangi Shaurya Priya
Age 23 22 21 20
Course B.tech B.tech B.sc B.sc
College Geu Geu Gehu Gehu
That’s all about how to create a pandas Dataframe from Dictionary