【发布时间】:2017-07-05 17:16:47
【问题描述】:
鉴于此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])}
但是我怎样才能创建一个字典,比如一个包含 first 和 second 作为键的元组?
结果是:
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...
【问题讨论】:
标签: python pandas dictionary tuples