2017-02-16 86 views
1

鑑於這種DataFrame的字典:熊貓:創建一個元組關鍵

import pandas as pd 
first=[0,1,2,3,4] 
second=[10.2,5.7,7.4,17.1,86.11] 
third=['a','b','c','d','e'] 
fourth=['z','zz','zzz','zzzz','zzzzz'] 
df=pd.DataFrame({'first':first,'second':second,'third':third,'fourth':fourth}) 
df=df[['first','second','third','fourth']] 

    first second third fourth 
0  0 10.20  a  z 
1  1 5.70  b  zz 
2  2 7.40  c zzz 
3  3 17.10  d zzzz 
4  4 86.11  e zzzzz 

我可以創建一個字典,作爲值列的列表,像這樣:

d = {df.loc[idx, 'first']: [df.loc[idx, 'second'], df.loc[idx, 'third']] for idx in range(df.shape[0])} 

卻怎麼也我創建了一個字典,例如包含firstsecond的元組作爲關鍵字?

其結果將是:

In[1]:d 
Out[1]: 
{(0,10.199999999999999): 'a', 
(1,5.7000000000000002): 'b', 
(2,7.4000000000000004): 'c', 
(3,17.100000000000001): 'd', 
(4,86.109999999999999): 'e'} 

PS:我怎麼能確保pandas不亂用值是多少? 10.20現已成爲10.1999999999 ...

回答

3

您需要通過set_index創建MultiIndex,然後調用Series.to_dict

a = df.set_index(['first','second']).third.to_dict() 
print (a) 
{(2, 7.4): 'c', (1, 5.7): 'b', (3, 17.1): 'd', (0, 10.2): 'a', (4, 86.11): 'e'}