简体   繁体   中英

pandas: get column and row name where row has the highest value for every row

I have the following dataframe:

import pandas as pd
data = pd.DataFrame({'sent':['one','two','three'], 'val_1':[2,4,8], 'val_2': [4,7,1], 'val_3':[9,3,6]})

I would like to get the rows that have the highest value along with the column name they appear in and the sent number as a list of dict, eg my desired output is,

output = [{'sent': 'one', 'val_3': 9}, {'sent': 'two', 'val_2': 7}, {'sent': 'three', 'val_1': 8}]

i have tried the following:

dict = data.to_dict('records')
for i in dict:
   for k,v in i.items():
          if not isinstance(v, str):
                 print(i, key =i.get) # sends an error 

I also tried to filter the max value but cannot get the column name to proceed.

data[['val_1','val_2','val_3']].max()

You could set 'sent' as index and use a list comprehension:

df = data.set_index('sent')
output = [{'sent': k, v: df.loc[k,v]} for k,v in df.idxmax(1).iteritems()]

output:

[{'sent': 'one', 'val_3': 9},
 {'sent': 'two', 'val_2': 7},
 {'sent': 'three', 'val_1': 8}]

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