Nesting a dictionary within another dictionary, grouping by values in a Pandas Dataframe

我的梦境 提交于 2019-12-14 02:09:52

问题


In this previous question: Nesting a counter within another dictionary where keys are dataframe columns , @Jezrael showed me how to nest a counter within another dictionary.

My dataframe has another column which is effectively a superset of the ID, and is not named in a way which allows for the SuperID to be logically derived from an ID.

SuperID   ID      Code
E1        E1023   a
E1        E1023   b
E1        E1023   b
E1        E1023   b
E1        E1024   b
E1        E1024   c
E1        E1024   c
E2        E1025   a
E2        E1025   a
E2        E1026   b

Using the dictionary which was produced in the last stage,

d = {k: v.value_counts().to_dict() for k, v in df.groupby('ID')['Code']}
print (d)

{'E1023': {'b': 3, 'a': 1}, 'E1024': {'c': 2, 'b': 1}, 'E1025 : {'a' : 2}, 
'E1026 : {'b' : 2}}

I would like to perform another level of nesting, where the SuperID is the key of the outer dictionary with the inner dictionary being the dictionary produced above, with IDs grouped by SuperID. So the dictionary should effectively be of the format:

new_d = {k: v for k in df.SuperID, v in df.groupby('SuperID')[ID FROM d]}

{'E1': {'E1023': {'b':3, 'a':1}, 'E1024' : {'c':2, 'b': 1}...} 'E2': {'E1025: {'a' : 2}...}}

I would like to keep the original dictionary, produced by @Jezrael to allow me to perform an easy lookup by ID which I will need to do at a latter stage.


回答1:


Use nested dictionary comprehension:

d = {k: {k1: v1.value_counts().to_dict() for k1, v1 in v.groupby('ID')['Code']} 
                                         for k, v in df.groupby('SuperID')}
print (d)

{'E1': {'E1023': {'b': 3, 'a': 1}, 'E1024': {'c': 2, 'b': 1}}, 
 'E2': {'E1025': {'a': 2}, 'E1026': {'b': 1}}}


来源:https://stackoverflow.com/questions/57694178/nesting-a-dictionary-within-another-dictionary-grouping-by-values-in-a-pandas-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!