Reputation: 43
I have a df with multiple columns. We need to check if any of the columns has a value 0; if 0 is there we have to add new column with tag.
I tried this :
df.loc[df['maths'] == '0', 'mark_student'] = 'fail'
This it working for single column, but when I tried it for multiple columns, it's failing
Like this :
df.loc[df['maths','chemistry'] == '0', 'mark_student'] = 'fail'
df.loc[df[['maths','chemistry']] == '0', 'mark_student'] = 'fail'
Upvotes: 1
Views: 3276
Reputation: 75
You have to use () each condition,
df.loc[(df['maths'] == 0) & (df['chemistry'] == 0) & (df['mark_student'] == 'fail')
Upvotes: 1
Reputation: 214957
df[['maths','chemistry']] == '0'
returns a data frame, which can't be directly used as boolean index at row level. You can use .any(1)
to reduce it to a Series and then use it with .loc
:
df.loc[(df[['maths','chemistry']] == '0').any(1), 'mark_student'] = 'fail'
Should work.
Upvotes: 3