pandas python pandas将数据帧转换为具有多个值的字典
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20112760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python pandas convert dataframe to dictionary with multiple values
提问by user2872701
I have a dataframe with 2 columns Address and ID. I want to merge IDs with the same addresses in a dictionary
我有一个包含 2 列地址和 ID 的数据框。我想在字典中合并具有相同地址的 ID
import pandas as pd, numpy as np
df = pd.DataFrame({'Address' : ['12 A', '66 C', '10 B', '10 B', '12 A', '12 A'],
'ID' : ['Aa', 'Bb', 'Cc', 'Dd', 'Ee', 'Ff']})
AS=df.set_index('Address')['ID'].to_dict()
print df
Address ID
0 12 A Aa
1 66 C Bb
2 10 B Cc
3 10 B Dd
4 12 A Ee
5 12 A Ff
print AS
{'66 C': 'Bb', '12 A': 'Ff', '10 B': 'Dd'}
What I want is for the duplicates to store multiple values like:
我想要的是重复存储多个值,如:
{'66 C': ['Bb'], '12 A': ['Aa','Ee','Ff'], '10 B': ['Cc','Dd']}
回答by DSM
I think you can use groupbyand a dictionary comprehension here:
我认为你可以groupby在这里使用字典理解:
>>> df
Address ID
0 12 A Aa
1 66 C Bb
2 10 B Cc
3 10 B Dd
4 12 A Ee
5 12 A Ff
>>> {k: list(v) for k,v in df.groupby("Address")["ID"]}
{'66 C': ['Bb'], '12 A': ['Aa', 'Ee', 'Ff'], '10 B': ['Cc', 'Dd']}
回答by lababidi
In response to the comment about multiple columns:
回应关于多列的评论:
>>> df
Address ID Name
0 12 A Aa Alpha
1 66 C Bb Bravo
2 10 B Cc Charlie
3 10 B Dd Delta
4 12 A Ee Edgar
5 12 A Ff Frank
>>> {k: v.to_dict() for k,v in df.groupby("Address")}

