将嵌套在两个词典下的列表转换为DataFrame

时间:2015-11-14 15:28:26

标签: python python-3.x dictionary pandas

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

我有一本字典,例如,它是一本跟踪课外学校课程的活动手册。在这种情况下,有两个课程,每个课程都是嵌套在活动书词典下的自己的词典。每个课程词典包含按月组织的每个人的活动列表。每月执行活动的学生数量是可变的,但结构始终是学生 - 活动 - 分钟。例如:

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 = Lesson#,ColB = Month,ColC = Student,ColD = Activity,ColE = Minutes。样本输出将是:

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

我找到了一种方法来创建C到C列的数据框,但是我无法包含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))

如何更新此项以包含字典键(课程和月份)作为列A和B?我不确定将列表列表更改为字典会有所帮助,但我将其保留为列表,因为这是我收到数据的方式。

1 个答案:

答案 0 :(得分:3)

使用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