Im trying to do the following to a pandas dataframe:
Input data:
Char A B
Nan 0 1
Nan 1 0
Output:
Char
B
A
As you can see I want to fill the value in 'Char' with a column name if the value of said column is 1. How should I approach this problem?
You can use
df['Char'] = df[['A','B']].idxmax(axis=1)
df
Out:
Char A B
0 B 0 1
1 A 1 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.