Reputation: 1998
I have code below as,
df['New_Column'] = df[['On Night Shift?']].apply(lambda x: any(x == 'Yes'), axis = 1)
which gives me a True or False value in the new column if the column value of On Night Shift? is Yes, if I want to scan multiple different columns for no, how would I tweak this code?
Something like below code,
df['New_Column'] = df[['On Night Shift?']].apply(lambda x: any(x == 'No'), axis = 1)
except I want to look if the columns On Day Shift?, On Mid Shift, On Weekend Shift? says no as well then give me True in the new column if all say no
Thanks!
Upvotes: 0
Views: 20
Reputation: 153510
Try this:
np.random.seed(1)
df = pd.DataFrame({'Number':np.arange(10), 'Day Shift':np.random.choice(['Yes', 'No'], 10),
'On Mid Shift':np.random.choice(['Yes', 'No'], 10),
'On Weekend Shift':np.random.choice(['Yes', 'No'], 10)})
df['No shifts'] = (df[['Day Shift', 'On Mid Shift', 'On Weekend Shift']] == 'No').all(axis=1)
print(df)
Output:
Number Day Shift On Mid Shift On Weekend Shift No shifts
0 0 No Yes Yes False
1 1 No No No True
2 2 Yes Yes Yes False
3 3 Yes No Yes False
4 4 No No No True
5 5 No Yes Yes False
6 6 No Yes Yes False
7 7 No No Yes False
8 8 No Yes No False
9 9 Yes Yes Yes False
Upvotes: 1