📌  相关文章
📜  Python|将嵌套字典列表转换为 Pandas 数据框

📅  最后修改于: 2022-05-13 01:54:33.085000             🧑  作者: Mango

Python|将嵌套字典列表转换为 Pandas 数据框

给定一个嵌套字典列表,编写一个Python程序来使用它创建一个 Pandas 数据框。让我们了解使用嵌套字典列表创建 Pandas Dataframe 的逐步过程。

步骤#1:创建嵌套字典列表。

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

输出:

步骤 #2:将 dict 值添加到行。

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

输出:
步骤#3:透视数据框并分配列名。

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

输出:


下面是完整的代码:

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

输出:

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