Home > Software engineering >  How do I get the name of the column with the highest value in a row pandas
How do I get the name of the column with the highest value in a row pandas

Time:06-12

print(df_merged)

          VolumeOIH  VolumeKRE   SPY close  difference  
    0       300.0    51852.0     430.00       -6.67  
    1       700.0    54027.0     437.85        5.63  
    2       500.0    46517.0     435.72       -4.31  
    3      5100.0    43994.0     432.22        0.66  
    4     12400.0    52993.0     419.45      -10.84  
    ..        ...        ...        ...         ...  
    64     8677.0    11411.0     409.62       -5.06  
    65      911.0    17334.0     417.37        8.51  
    66     1990.0    16353.0     410.54       -2.34  
    67     1687.0    13611.0     411.77       -2.54  
    68     2307.0    11291.0     415.70        6.72  

As you can see my columns have different names. I want to create a new column that will simply show the name of the column with the highest volume.

If we take line 0, the highest volume is VolumeKRE. I want a new column to be created where the value on the first row would show : VolumeKRE.

CodePudding user response:

The following command will give you a column with the name of the maximum column:

df_merged.idxmax(axis=1)

Then you can merge it to your original dataframe

  • Related