How to apply multiple conditions in a dataframe

85 Views Asked by At

I do have an issue while applying a condition and then applying a value to that particular values. I'm new to this just practicing with dataset. My question is I need to apply a bonus mark to those students who have secured above 77 marks for 'math'.

Any suggestion would be helpful.

I have used if condition to check if the mark is above 77 and then created a column giving bonus mark to those. What I have tried is :The above image gives the values for those students who have secured more than 77 in 'math_score' Now, I need to give bonus score to these students, what I tried is that I looped through each student and then wrote a if condition gives me an error as '

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Please help me to achieve this goal, do correct me too. This image shows the result for the condityion, who have secured score more than 77 This is the error, I have got when I applied my condition

1

There are 1 best solutions below

0
On

You can use np.where():

import numpy as np
df['bonus']=np.where(df['math_score'] > 77,(df['math_score'] + df['reading_score'] + df['writing_socre'] + 10),(df['math_score'] + df['reading_score'] + df['writing_socre']))