Python | Convert list of nested dictionary into Pandas dataframe
Given a list of nested dictionary, write a Python program to create a Pandas dataframe using it. Let’s understand stepwise procedure to create Pandas Dataframe using list of nested dictionary.
Step #1: Creating a list of nested dictionary.
# importing pandasimport pandas as pd # List of nested dictionary initializationlist = [ { "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:
Step #2: Adding dict values to rows.
# rows list initializationrows = [] # appending rowsfor data in list: data_row = data['Student'] time = data['Name'] for row in data_row: row['Name']= time rows.append(row) # using data framedf = pd.DataFrame(rows) # print(df) |
Output:
Step #3: Pivoting dataframe and assigning column names.
# using pivot_tabledf = df.pivot_table(index ='Name', columns =['Grade'], values =['Exam']).reset_index() # Defining columnsdf.columns =['Name', 'Maths', 'Physics', 'Chemistry'] # print dataframeprint(df) |
Output:
Below is the complete code:
# Python program to convert list of nested # dictionary into Pandas dataframe # importing pandasimport pandas as pd # List of list of dictionary initializationlist = [ { "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 list initializationrows = [] # appending rowsfor data in list: data_row = data['Student'] time = data['Name'] for row in data_row: row['Name']= time rows.append(row) # using data framedf = pd.DataFrame(rows) # using pivot_tabledf = df.pivot_table(index ='Name', columns =['Grade'], values =['Exam']).reset_index() # Defining columnsdf.columns =['Name', 'Maths', 'Physics', 'Chemistry'] # print dataframeprint(df) |
Output:
Name Maths Physics Chemistry 0 Chunky Pandey 89 80 NaN 1 Paras Jain 90 99 97
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course


