mardi 27 octobre 2020

How to assign a new column based on conditions of other columns?

I am trying to add a column, "flag_column" based on the value present in A, B, C, D.

i.e if there is a value in A / B / C / D I would like to create a new column, 'flag' indicating the column name that contains a value.

  A B C D counts flag
0 1 0 0 0  1     A
1 0 1 0 0  1     B
2 1 0 0 0  1     A
3 0 0 1 0  1     C
4 0 1 0 0  1     B

Note: There will only be one column (A through D) that contains a value, so counts will always be 1.

I've tried:

if [df['A'] == 1] == True:
    df['flag'] = 'A'
elif [df['B'] == 1] == True:
    df['flag'] = 'B'
elif [df['C'] == 1] == True:
    df['flag'] = 'C'  
else:
    df['flag'] = 'D'    

I have also tried:

df['flag'] = np.where(df['A'] == 1, 'A', False)
df['flag'] = np.where(df['B'] == 1, 'B', False)
df['flag'] = np.where(df['C'] == 1, 'C', False)
df['flag'] = np.where(df['D'] == 1, 'D', False)

I've also tried doing this iteratively looping through each "category" and assigning a flag value, however it overwrites in these cases as well.

If there is a way in which I could iteratively do this, that would be ideal. However, any help on this (simple) question would be greatly appreciated!

Aucun commentaire:

Enregistrer un commentaire