1

I have the following dataframe:

>>> name  age  grade  pass
0   Dana  23    95    yes
1   Emaa  24          yes
2   Don   25    99    yes
3 Daniela 24    85    yes
4   Fred  21          yes

I want to create a condition so if column grade is null, the value in column pass will be no :

>>> name  age  grade  pass
0   Dana  23    95    yes
1   Emaa  24          no
2   Don   25    99    yes
3 Daniela 24    85    yes
4   Fred  21          no

I have tried this way:

if df['grade'].isna():
    df['pass']='no'

but I get error:

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

how can I get the expected results? what am I doing wrong?

Reut
  • 1,555
  • 4
  • 23
  • 55

1 Answers1

2

You need to do np.where or mask

df['pass'] = df['pass'].mask(df['grade'].isna(), 'no')
BENY
  • 317,841
  • 20
  • 164
  • 234
  • Thank you for sharing this Ben, could you please do let me know how you created `df` from samples? I tried to copy and use `read_clipboard` but then its not taking space in between and last field its making as `NaN`, any guidance will be appreciated here. – RavinderSingh13 Sep 14 '20 at 17:11
  • 1
    @RavinderSingh13 I have not create the dataframe ~ , just based on his expected output and the solution should be pretty obviously ~ – BENY Sep 14 '20 at 17:32