Given a list of the nested dictionary, write a Python program to create a Pandas dataframe using it. Let’s understand the stepwise procedure to create a Pandas Dataframe using the list of nested dictionary.
Step #1: Creating a list of nested dictionary.
Python3
import pandas as pd
list = [{
"Student": [{"Exam": 90, "Grade": "a"},
{"Exam": 99, "Grade": "b"},
{"Exam": 97, "Grade": "c"},
],
"Name": "Paras Jain"
},
{
"Student": [{"Exam": 89, "Grade": "a"},
{"Exam": 80, "Grade": "b"}
],
"Name": "Chunky Pandey"
}
]
print(list)
Output:
[{'Student': [{'Exam': 90, 'Grade': 'a'}, {'Exam': 99, 'Grade': 'b'}, {'Exam': 97, 'Grade': 'c'}], 'Name': 'Paras Jain'}, {'Student': [{'Exam': 89, 'Grade': 'a'}, {'Exam': 80, 'Grade': 'b'}], 'Name': 'Chunky Pandey'}]
Step #2: Adding dict values to rows.
Python3
rows = []
for data in list:
data_row = data['Student']
time = data['Name']
for row in data_row:
row['Name']= time
rows.append(row)
df = pd.DataFrame(rows)
print(df)
Output:
Exam Grade Name 0 90 a Paras Jain 1 99 b Paras Jain 2 97 c Paras Jain 3 89 a Chunky Pandey 4 80 b Chunky Pandey
Step #3: Pivoting DataFrame and assigning column names.
Python3
df = df.pivot_table(index ='Name', columns =['Grade'],
values =['Exam']).reset_index()
df.columns =['Name', 'Maths', 'Physics', 'Chemistry']
print(df)
Output:
Name Maths Physics Chemistry 0 Chunky Pandey 89.0 80.0 NaN 1 Paras Jain 90.0 99.0 97.0
Below is the complete code :
Python3
import pandas as pd
list = [
{
"Student": [{"Exam": 90, "Grade": "a"},
{"Exam": 99, "Grade": "b"},
{"Exam": 97, "Grade": "c"},
],
"Name": "Paras Jain"
},
{
"Student": [{"Exam": 89, "Grade": "a"},
{"Exam": 80, "Grade": "b"}
],
"Name": "Chunky Pandey"
}
]
rows = []
for data in list:
data_row = data['Student']
time = data['Name']
for row in data_row:
row['Name']= time
rows.append(row)
df = pd.DataFrame(rows)
df = df.pivot_table(index ='Name', columns =['Grade'],
values =['Exam']).reset_index()
df.columns =['Name', 'Maths', 'Physics', 'Chemistry']
print(df)
Output:
Name Maths Physics Chemistry 0 Chunky Pandey 89 80 NaN 1 Paras Jain 90 99 97
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
31 Jul, 2023
Like Article
Save Article