i have a data frame
A B C True False False False False True True False False False False False
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
result A C A na
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) :
df['result'] = df.idxmax(1).where(df.any(1))
Or for a numpy based one you can use argmax instead:
import numpy as np df['result'] = np.where(df.values.any(1), df.columns[df.values.argmax(1)], np.nan)
print(df)
A B C result
0 True False False A
1 False False True C
2 True False False A
3 False False False NaN