Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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, e.g 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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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}]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading