如果一列中的值为空,则在另一列中给出另一个值

If value in one column is null give another value in another column

我有以下数据框:

>>> 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

我想创建一个条件,如果列等级为空,则列 pass 中的值将为 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

我试过这种方法:

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

但我收到错误:

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

怎样才能得到预期的结果?我做错了什么?

你需要做np.wheremask

df['pass'] = df['pass'].mask(df['grade'].isna(), 'no')