Skip to content
Advertisement

Pandas: getting the name of the minimum column

I have a Pandas dataframe as below:

JavaScript

I want to append a reason column that gives a standard text + the column name of the minimum value of that row. In other words, the desired output is:

JavaScript

I can do incomplete_df.apply(lambda x: min(x),axis=1) but this does not ignore NAN‘s and more importantly returns the value rather than the name of the corresponding column.

EDIT:

Having found out about the idxmin() function from EMS’s answer, I timed the the two solutions below:

JavaScript

It seems like the transpose approach is twice as fast.

Advertisement

Answer

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement