how to get the column name from multiple column into one if its true in pyhton?
Question:
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
Answers:
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
You could simply iterate on the columns in reverse order:
for col in reversed(df.columns):
df.loc[df[col],'result'] = col
It gives:
A B C result
0 True False False A
1 False False True C
2 True False False A
3 False False False NaN
IIUC dot
df.dot(df.columns)
0 A
1 C
2 A
3
dtype: object
Update pandas 1.5+ pd.from_dummies
:
pd.from_dummies(df, default_category='na')
Output:
0 A
1 C
2 A
3 na
Therfore,
df['result'] = pd.from_dummies(df, default_category='na')
df
Output:
A B C result
0 True False False A
1 False False True C
2 True False False A
3 False False False na
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
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
You could simply iterate on the columns in reverse order:
for col in reversed(df.columns):
df.loc[df[col],'result'] = col
It gives:
A B C result
0 True False False A
1 False False True C
2 True False False A
3 False False False NaN
IIUC dot
df.dot(df.columns)
0 A
1 C
2 A
3
dtype: object
Update pandas 1.5+ pd.from_dummies
:
pd.from_dummies(df, default_category='na')
Output:
0 A
1 C
2 A
3 na
Therfore,
df['result'] = pd.from_dummies(df, default_category='na')
df
Output:
A B C result
0 True False False A
1 False False True C
2 True False False A
3 False False False na