Skip to content
Advertisement

how to get the column name from multiple column into one if its true in pyhton?

i have a data frame

JavaScript

I want to get column named “result” which will return the column name if it is true and nan, if any of it is not True.

Expected Column

JavaScript

Advertisement

Answer

You could use Series.where to set the column value to either the idxmax along the second axis, so the respective column name of the first True, or NaN, depending on the result of df.any(1) :

JavaScript

Or for a numpy based one you can use argmax instead:

JavaScript

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