I have a dataframe:
var1 var2 var3 var4
Id#
1001 Y Y Y Y
1002 N N N N
1003 N N Y N
1003 Y Y Y N
I want to create a new column called Small, where if any var=Y then Small is equal to N
var1 var2 var3 var4 Small
Id#
1001 Y Y Y Y N
1002 N N N N Y
1003 N N Y N N
1003 Y Y Y N N
My tried solution: I have created a function called is_small that flips to 'N' anytime there a column in a row is 'Y'
def is_small(row, *cols):
_small = 'Y'
for col in cols:
if col == 'Y':
_small = 'N'
return _small
and apply it to my dataset:
all_data['Small'] = all_data.apply(lambda row: is_small(row,
'var1',
'var2',
'var3',
'var4'),
axis=1)
However Small just all return as 'Y' and I'm not sure why.