Pandas Dataframe to dict grouping by column

2019-07-27 21:57发布

I have a dataframe like this:

Subject_id    Subject    Score    
Subject_1        Math        5                 
Subject_1    Language        4                 
Subject_1       Music        8
Subject_2        Math        8                 
Subject_2    Language        3                 
Subject_2       Music        9

And I want to convert it into a dictionary, grouping by subject_id

{'Subject_1': {'Math': 5,
               'Language': 4,
               'Music': 8},
{'Subject_2': {'Math': 8,
               'Language': 3,
               'Music': 9}
}

If I would have only one Subject, then I could so:

my_dict['Subject_1'] = dict(zip(df['Subject'],df['Score']))

But since I have several subjects the list of keys repeats, so I cannot use directly a zip.

Dataframes has a .to_dict('index') method but I need to be able to group by a certain column when creating the dictionary.

How could I achieve that?

Thanks.

2条回答
Rolldiameter
2楼-- · 2019-07-27 22:27

Use groupby with custom lambda function and last convert output Series to_dict:

d = (df.groupby('Subject_id')
       .apply(lambda x: dict(zip(x['Subject'],x['Score'])))
       .to_dict())

print (d)
{'Subject_2': {'Math': 8, 'Music': 9, 'Language': 3}, 
 'Subject_1': {'Math': 5, 'Music': 8, 'Language': 4}}

Detail:

print (df.groupby('Subject_id').apply(lambda x: dict(zip(x['Subject'],x['Score']))))

Subject_id
Subject_1    {'Math': 5, 'Music': 8, 'Language': 4}
Subject_2    {'Math': 8, 'Music': 9, 'Language': 3}
dtype: object
查看更多
神经病院院长
3楼-- · 2019-07-27 22:29

Use to_dict with pivot

In [29]: df.pivot('Subject_id', 'Subject', 'Score').to_dict('index')
Out[29]:
{'Subject_1': {'Language': 4L, 'Math': 5L, 'Music': 8L},
 'Subject_2': {'Language': 3L, 'Math': 8L, 'Music': 9L}}

Or,

In [25]: df.set_index(['Subject_id', 'Subject']).unstack()['Score'].to_dict('index')
Out[25]:
{'Subject_1': {'Language': 4L, 'Math': 5L, 'Music': 8L},
 'Subject_2': {'Language': 3L, 'Math': 8L, 'Music': 9L}}
查看更多
登录 后发表回答