'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:
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
