+1 vote
in Programming Languages by (71.8k points)

I read a CSV file using the read_csv() function of pandas. I want to find the columns that contain the maximum value for each row. Each row can have different columns with the maximum value. Is there any pandas function similar to argmax() of Numpy?

1 Answer

+1 vote
by (351k points)
selected by
 
Best answer

You can use the idxmax() method of pandas. It returns the index of the first occurrence of maximum value over the specified axis. Since you want the column with the maximum value in each row, you can use axis="columns" or axis=1 as the argument to this method.

Here is an example:

>>> df=pd.DataFrame({'a':np.random.randint(1,100,11), 'b':np.random.randint(1,100,11),'c':np.random.randint(1,100,11)})

>>> df

     a   b   c

0   25  45  85

1   67  59  96

2   11  45   8

3   51  10  16

4   61  38  46

5   75  96  46

6    2  87  90

7   52  57  87

8   59  82  35

9   65  83  95

10  87  74  71

>>> df.idxmax(axis=1)

0     c

1     c

2     b

3     a

4     a

5     b

6     c

7     c

8     b

9     c

10    a

dtype: object

>>> df.idxmax(axis="columns")

0     c

1     c

2     b

3     a

4     a

5     b

6     c

7     c

8     b

9     c

10    a

dtype: object


...