简体   繁体   中英

python dictionary with multiple keys as tuple and df values

Below id my DataFrame named 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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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