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

Time:11-24

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]

CodePudding user response:

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]
  • Related