2013-11-21 82 views
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']} 

回答

14

我認爲你可以使用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']} 
+0

感謝我所需要的 – user2872701

+1

什麼是我有多個列,我需要df.groupby('Adress')['ID','XX']? – user815408

0

在回答關於多列註釋:

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