本文介绍了将一个字典嵌套在另一个字典中,并按Pandas Dataframe中的值分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上一个问题中:,@ Jezrael向我展示了如何在另一个字典中嵌套一个计数器.

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.

我的数据框具有另一列,该列实际上是ID的超集,并且没有以允许从ID逻辑上导出SuperID的方式命名.

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}}

我想执行另一层嵌套,其中SuperID是外部字典的关键字,内部字典是上面生成的字典,其ID由SuperID分组.因此,字典应有效地采用以下格式:

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}...}}

我想保留@Jezrael制作的原始字典,以便我可以在以后的阶段中通过ID轻松地进行查找.

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.

推荐答案

使用嵌套字典理解:

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}}}

这篇关于将一个字典嵌套在另一个字典中,并按Pandas Dataframe中的值分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 05:44