1

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]

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.