I have a Pandas Dataframe and wish to reverse the binary encoding (i.e. get_dummies()
) of three columns. The encoding is left-to-right:
a b c
0 0 1 1
1 0 0 1
2 1 1 1
3 1 0 0
would result in a new categories column C
taking values 0-7
:
C
1 6
2 4
3 7
4 1
I am not sure why this line is giving me a syntax error, near axis=1
:
df['C'] = df.apply(lambda x: (x['a']==1 ? 1:0)+(x['b']==1 ? 2:0)+(x['c']==1 ? 4:0), axis=1)