简体   繁体   中英

Creating a dictionary from DataFrame column and values

I have a DataFrame that looks like this:

     A     B     C      D
0    One   Two   Three  Four
1    31    47    44     22
2    53    38    11     27
3    86    84    81     87
4    57    4     23     46

I want to create a loop that will give me key of one, two, three, with values of 53, 47, 44, 22 going down. So first dict would be

 {'One': 31, 'Two': 47, 'Three': 44, 'Four' : 22}

next one

{'One': 53, 'Two': 38, 'Three': 11, 'Four' : 22}

and so on.

I have completly no clue how to accomplish it. Any ideas?

You can use DataFrame.to_dict() in order to accomplish it.

You can find more about the topic on:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_dict.html

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