简体   繁体   English

将 Pandas 数据框从多列导出到嵌套字典

[英]Export pandas dataframe to a nested dictionary from multiple columns

Its a similar question to this here这是一个类似的问题here

But in this case I want something different.但在这种情况下,我想要不同的东西。 I have the next dataframe example:我有下一个数据框示例:

from pandas import DataFrame

df = DataFrame([
           ['A', 123, 1], 
           ['B', 345, 5], 
           ['C', 712, 4],
           ['B', 768, 2], 
           ['B', 768, 3], 
           ['A', 123, 9], 
           ['C', 178, 6], 
           ['C', 178, 5],  
           ['A', 321, 3]], 
           columns=['maingroup', 'subgroup', 'selectedCol'])

And I want to extract a nested dictionary, where the main keys correspond to the unique names in 'maingroup' , and the subkeys correspond to unique names of 'subgroup' and the values store arrays of values from 'selectedCol' with common 'maingroup' and 'subgroup' keys, like so:我想提取一个嵌套字典,其中主键对应于'maingroup'的唯一名称,子键对应于'subgroup'唯一名称,并且值存储来自'selectedCol'的值数组和公共'maingroup''subgroup'键,如下所示:

{
 'A': {'123':[1, 9], '321':[3]},
 'B': {'345':[5], '768':[2, 3]},
 'C': {'712':[4], '178':[6, 5]}
}

Using dict comprehension with nested groupby:使用嵌套 groupby 的字典理解:

d = {k: f.groupby('subgroup')['selectedCol'].apply(list).to_dict()
     for k, f in df.groupby('maingroup')}

[out] [出去]

{'A': {123: [1, 9], 321: [3]},
 'B': {345: [5], 768: [2, 3]},
 'C': {178: [6, 5], 712: [4]}}

Create MultiIndex Series and then in dictionary comprehension nested dict s:创建MultiIndex Series ,然后在字典理解中嵌套dict s:

s = df.groupby(['maingroup','subgroup'], sort=False)['selectedCol'].apply(list)
d = {l: s.xs(l).to_dict() for l in s.index.levels[0]}
print (d)
{'A': {123: [1, 9], 321: [3]}, 
 'B': {345: [5], 768: [2, 3]}, 
 'C': {712: [4], 178: [6, 5]}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM