10
我有一個包含2列地址和ID的數據幀。我想合併具有相同地址的ID在字典python熊貓將數據幀轉換爲具有多個值的字典
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'}
我要的是重複的存儲像多個值:
{'66 C': ['Bb'], '12 A': ['Aa','Ee','Ff'], '10 B': ['Cc','Dd']}
感謝我所需要的 – user2872701
什麼是我有多個列,我需要df.groupby('Adress')['ID','XX']? – user815408