Related Articles

Related Articles

Python | Convert list of nested dictionary into Pandas dataframe
  • Last Updated : 14 May, 2020

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

 

Step #2: Adding dict values to rows.

filter_none

edit
close

play_arrow

link
brightness_4
code

# rows list initialization
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)

chevron_right


Output:

 
Step #3: Pivoting dataframe and assigning column names.



filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

 
Below is the complete code:

filter_none

edit
close

play_arrow

link
brightness_4
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 initialization
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)

chevron_right


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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :