How to create new column based on value in a set of columns

I have a pandas df like this:

time   a    b    c  
1      0    1    0
1      0    1    0
1      1    0    0
1      0    1    0
1      0    0    1
1      0    0    0

I want to create a new column, df.code based on the following logic:

  • if df.a == 1, return 4
  • if df.b == 1, return 2
  • if df.c == 1, return 1
  • if a,b, or c != 1, return 0
time   a    b    c    code  
1      0    1    0     2
1      0    1    0     2 
1      1    0    0     4
1      0    1    0     2
1      0    0    1     1
1      0    0    0     0

How do I do this? I’m essentially trying to compress select dummy columns into a multiclass columns.

Answer

This example should works as is:

stack.csv

time a b c
1 0 1 0
1 0 1 0
1 1 0 0
1 0 1 0
1 0 0 1
1 0 0 0

main.py

df = pd.read_csv('stack.csv', sep=' ', index_col=False)

df['code'] = 0

df.loc[df['a'] == 1, 'code'] = 4
df.loc[df['b'] == 1, 'code'] = 2
df.loc[df['c'] == 1, 'code'] = 1

print(df)

output:

       time  a  b  c  code
0     1  0  1  0     2
1     1  0  1  0     2
2     1  1  0  0     4
3     1  0  1  0     2
4     1  0  0  1     1
5     1  0  0  0     0