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 pandas import pandas as pd # List of nested dictionary initialization 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:

Step #2: Adding dict values to rows.
# rows list intialization rows = [] # appending rows for data in list: data_row = data['Student'] time = data['Name'] for row in data_row: row['Name']= time rows.append(row) # using data frame df = pd.DataFrame(rows) # print(df) |
Output:

Step #3: Pivoting dataframe and assigning column names.
# using pivot_table df = df.pivot_table(index ='Name', columns =['Grade'], values =['Exam']).reset_index() # Defining columns df.columns =['Name', 'Maths', 'Physics', 'Chemistry'] # print dataframe print(df) |
Output:

Below is the complete code:
# Python program to convert list of nested # dictionary into Pandas dataframe # importing pandas import pandas as pd # List of list of dictionary initialization 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 list intialization rows = [] # appending rows for data in list: data_row = data['Student'] time = data['Name'] for row in data_row: row['Name']= time rows.append(row) # using data frame df = pd.DataFrame(rows) # using pivot_table df = df.pivot_table(index ='Name', columns =['Grade'], values =['Exam']).reset_index() # Defining columns df.columns =['Name', 'Maths', 'Physics', 'Chemistry'] # print dataframe print(df) |
Output:
Name Maths Physics Chemistry 0 Chunky Pandey 89 80 NaN 1 Paras Jain 90 99 97
Recommended Posts:
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert a nested list into a flat list
- Python | Convert given list into nested list
- Python | Convert a list to dictionary
- Python | Convert dictionary to list of tuples
- Python | Convert list of tuple into dictionary
- Python | Convert a list of Tuples into Dictionary
- Python | Convert list of tuples to dictionary value lists
- Make a Pandas DataFrame with two-dimensional list | Python
- Create pandas dataframe from lists using dictionary
- Using dictionary to remap values in Pandas DataFrame columns
- Python | Check if a nested list is a subset of another nested list
- Python | Nested Dictionary
- Python | Sort nested dictionary by key
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



