gpt4 book ai didi

python - 将嵌套在两个字典下的列表转换为 DataFrame

转载 作者:行者123 更新时间:2023-11-30 23:04:28 33 4
gpt4 key购买 nike

我正在尝试使用 Pandas 在 Python 中创建一个数据框架,其中涉及嵌套字典和列表列表。我查看了有关转换嵌套字典的其他问题,但找不到足够的答案。

我有一本字典,例如,它是一本记录课外学校类(class)的事件书。在本例中,有两个类(class),每个类(class)都是嵌套在事件书字典下的自己的字典。每个类(class)词典都包含每个人的事件列表,按月份组织。每月进行某项事件的学生数量是可变的,但结构始终是学生-事件-分钟。例如:

activity_dict = {

'lesson1' : { 'january' : [['Todd', 'Running', 30],['Christy', 'Studying', 25],['Alex','Soccer', 10]],
'february' : [['Jim', 'Bobsledding', 5],['Frank', 'Jogging',8]]},

'lesson2' : {'february' : [['Todd', 'Running', 18],['John', 'Studying', 3],['Don','Soccer', 40]],
'march' : [['Tom', 'Bobsledding', 10],['Sam', 'Yoga', 42]],
'april' : [['Julie', 'Biking', 20],['Chris', 'Baseball', 10]]}
}

我试图获得每个学生事件的输出,ColA = 类(class)编号,ColB = 月份,ColC = 学生,ColD = 事件,ColE = 分钟。示例输出为:

Lesson # Month Student Activity Minutes
Lesson 1 February Jim Bobsledding 5
Lesson 1 February Frank Jogging 8
Lesson 2 February Todd Running 18

我找到了一种创建 C 列到 E 列数据框的方法,但无法包含 A 列和 B 列。

我现在的代码如下:

import pandas

activity_log = []

for lesson, all_activities in activity_dict.items():
for month, month_activities in all_activities.items():
activity_log.append(pandas.DataFrame(month_activities))

如何更新它以将字典键(类(class)和月份)包含为 A 列和 B 列?我不确定将列表列表更改为字典是否有帮助,但我将其保留为列表,因为这就是我接收数据的方式。

最佳答案

使用 list comprehension将列表列表的字典的字典转换为列表的列表:

In [99]: [(lesson, month, name, activity, minutes) 
for lesson, dct in activity_dict.items()
for month, vals in dct.items()
for name, activity, minutes in vals]
Out[99]:
[('lesson2', 'april', 'Julie', 'Biking', 20),
('lesson2', 'april', 'Chris', 'Baseball', 10),
('lesson2', 'february', 'Todd', 'Running', 18),
('lesson2', 'february', 'John', 'Studying', 3),
('lesson2', 'february', 'Don', 'Soccer', 40),
('lesson2', 'march', 'Tom', 'Bobsledding', 10),
('lesson2', 'march', 'Sam', 'Yoga', 42),
('lesson1', 'january', 'Todd', 'Running', 30),
('lesson1', 'january', 'Christy', 'Studying', 25),
('lesson1', 'january', 'Alex', 'Soccer', 10),
('lesson1', 'february', 'Jim', 'Bobsledding', 5),
('lesson1', 'february', 'Frank', 'Jogging', 8)]

然后使用pd.DataFrame从列表列表中构建DataFrame:

In [98]: pd.DataFrame([(lesson, month, name, activity, minutes)
for lesson, dct in activity_dict.items()
for month, vals in dct.items()
for name, activity, minutes in vals],
columns=['Lesson', 'Month', 'Name', 'Activity', 'Minutes'])
Out[98]:
Lesson Month Name Activity Minutes
0 lesson2 april Julie Biking 20
1 lesson2 april Chris Baseball 10
2 lesson2 february Todd Running 18
3 lesson2 february John Studying 3
4 lesson2 february Don Soccer 40
5 lesson2 march Tom Bobsledding 10
6 lesson2 march Sam Yoga 42
7 lesson1 january Todd Running 30
8 lesson1 january Christy Studying 25
9 lesson1 january Alex Soccer 10
10 lesson1 february Jim Bobsledding 5
11 lesson1 february Frank Jogging 8

关于python - 将嵌套在两个字典下的列表转换为 DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33709983/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com