简体   繁体   中英

Converting pandas data frame to dictionary in python

My pandas dataframe (df):

Pattern      Support

Bread         4
Milk          4
Bread,Milk    4

My code:

x = df.set_index('Pattern').to_dict()
print(x)

Output of my code:

{'Support':{'MILK': 4,'BREAD':4, 'BREAD,MILK':4}}

Expected Output:

{'MILK': 4,'BREAD':4, 'BREAD,MILK':4}

I do not want the name of the second column 'Support' to be the main key, just want to get the elements and the count as in the expected output. Is there any pythonic (python 3.5) way?

在一行中打包太多,但是它将为您提供所需的东西。

list(df.set_index('Pattern').to_dict().values()).pop()

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