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

How do I get the name of the column with the highest value in a row pandas

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.

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

>Solution :

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

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