简体   繁体   English

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

[英]python dictionary with multiple keys as tuple and df values

Below id my DataFrame named df2 在id下面的我的DataFrame名为df2

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

0,1,1,0,0,0 0,1,1,0,0,0

1,1,1,1,0,1 1,1,1,1,0,1

2,1,1,0,1,1 2,1,1,0,1,1

3,1,0,1,0,1 3,1,0,1,0,1

I want to create a dictionary such that the first row and index become tuple and the values become values of the dictionary, something like 我想创建一个字典,使第一行和索引成为元组,并且值成为字典的值,例如

{ {

 (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,
}

My trial so far your_dict = dict(zip(es, df2)) print(your_dict) but,does not produce my desired output 到目前为止,我的试用版your_dict = dict(zip(es,df2))print(your_dict)但是,没有产生我想要的输出

Create every combination of indices/columns, then create a dictionary with the key as a tuple of these values and the value is the value in the dataframe at that postion. 创建索引/列的每种组合,然后使用键作为这些值的元组创建字典,该值是该位置数据框中的值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM