简体   繁体   English

迭代df并输出带有值列表的字典

[英]iterate over df and output dictionary with list of values

I need to convert a dataframe to a dictionary but can't get all of the values from the dataframe to appear within the dictionary. 我需要将数据帧转换为字典,但无法从数据框中获取所有值以显示在字典中。

dataframe: 数据帧:

id| region | Num | 
--|--------|-----|
2 | NYC    |2344 |
3 | NYC    |3243 |
4 | NYC    |3253 |
5 | NYC    |2345 |
6 | CHI    |8756 |
7 | CHI    |9786 |
8 | CHI    |7674 |
9 | CHI    |6678 |
10| ATL    |1234 |

code: 码:

df.set_index('region').T.to_dict('list') : df.set_index('region').T.to_dict('list')

What I need is this: 我需要的是这个:

{'NYC: [2344, 3243, 3253, 2345 ], 'CHI': [8756, 9786, 7674, 6678], 'ATL': [1234] }

but what I'm getting is this: 但我得到的是这个:

{'NYC: [2345 ], 'CHI': [6678], 'ATL': [1234] }

I tried: 我试过了:

    num_dict = {}
    for region, num in df:
        num_dict.setdefault(region, []).append(num)

But this gives me a (ValueError: too many values to unpack (expected 2) . 但是这给了我一个(ValueError: too many values to unpack (expected 2)

Is there a better way to do this? 有一个更好的方法吗?

Thanks in advance! 提前致谢!

Use groupby.apply(list) then .to_dict 使用groupby.apply(list)然后使用.to_dict

df.groupby('region')['Num'].apply(list).to_dict()

[out] [OUT]

{'ATL': [1234],
 'CHI': [8756, 9786, 7674, 6678],
 'NYC': [2344, 3243, 3253, 2345]}

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

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