Monday, September 23, 2024
Google search engine
HomeLanguagesPython | Convert list of nested dictionary into Pandas dataframe

Python | Convert list of nested dictionary into Pandas dataframe

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




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

[{'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 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)


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




# 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.0 80.0 NaN
1 Paras Jain 90.0 99.0 97.0

Below is the complete code:

Python3




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


Output:

            Name  Maths  Physics  Chemistry
0 Chunky Pandey 89 80 NaN
1 Paras Jain 90 99 97

RELATED ARTICLES

Most Popular

Recent Comments