带有多个键的元组和df值的python字典

时间:2019-03-04 17:39:36

标签: python dataframe dictionary

在ID下面的我的DataFrame名为df2

ID,'AI','DB','ML','Python','IR'

0,1,1,0,0,0

1,1,1,1,0,1

2,1,1,0,1,1

3,1,0,1,0,1

我想创建一个字典,使第一行和索引成为元组,并且值成为字典的值,类似

{

 (0,"AI"):1,(0,"DB"):1,(0,"ML"):0,(0,"Python"):0,(0,"IR"):0,
 (1,"AI"):1,(1,"DB"):1,(1,"ML"):1,(1,"Python"):0,(1,"IR"):0,
 (2,"AI"):1,(2,"DB"):1,(2,"ML"):1,(2,"Python"):0,(2,"IR"):1,
 (3,"AI"):1,(3,"DB"):0,(3,"ML"):1,(3,"Python"):0,(3,"IR"):1,
}

到目前为止,我的审判your_dict = dict(zip(es,df2)) print(your_dict)但是不会产生我想要的输出

1 个答案:

答案 0 :(得分:2)

创建索引/列的每个组合,然后创建一个字典,并使用键作为这些值的元组,并且该值是该位置数据框中的值。

import itertools
{(x,y):df[y][x] for x, y in itertools.product(df.index, df.columns)}