7

I am trying to find the pandas equivalent of this question.

For each row return the column name of the largest value

I want to add a new column to the below dataframe which is the column name of the lowest value in each row.

   Multi-Use  Charging  Performer  Controls  Value for Money  All Rounder
0   1.569541  0.290916   2.396734  0.881500         3.171563     1.950175
1   0.906542  2.296172   0.162809  1.604936         0.730633     0.532835
2   0.442924  0.970764   1.264364  0.295140         2.034826     0.824529
3   0.167663  1.367973   0.877306  0.683562         1.653964     0.444136
4   0.870290  0.547844   1.703054  0.209975         2.476787     1.260371

getting min is simple: df.iloc[:, 0:6].min(axis=1)

how do I return the column name based on the min?

vagabond
  • 3,526
  • 5
  • 43
  • 76

1 Answers1

13

You can do

df['lowest_col'] = df.idxmin(axis=1)

You get

    Multi-Use   Charging    Performer   Controls    Value for Money All Rounder lowest_col
0   1.569541    0.290916    2.396734    0.881500    3.171563    1.950175    Charging
1   0.906542    2.296172    0.162809    1.604936    0.730633    0.532835    Performer
2   0.442924    0.970764    1.264364    0.295140    2.034826    0.824529    Controls
3   0.167663    1.367973    0.877306    0.683562    1.653964    0.444136    Multi-Use
4   0.870290    0.547844    1.703054    0.209975    2.476787    1.260371    Controls
Vaishali
  • 37,545
  • 5
  • 58
  • 86