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

Python: Get Columns of max values each row of an pandas dataframe

What is the best way in python to get the column number of the max values of each row in a pandas dataframe-matrix (without the index column). E.g. if I would have

Date Company 1 Company 2 Company 3
01.01.2020 23 21 14
02.01.2020 22 12 22
03.01.2020 11 11 12
….
02.01.2020 2 14 3

The output should be the vector:

 [1, 1, 3, ..., 2]

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 :

Use idxmax:

In [949]: cols = df.iloc[:, 1:].idxmax(1).tolist()

In [950]: cols
Out[950]: ['Company 1', 'Company 1', 'Company 3']

If you want column index:

In [951]: [df.columns.get_loc(i) for i in cols]
Out[951]: [1, 1, 3]
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