dimanche 13 septembre 2020

change specific values in dataframe if one cell in a row is null

I have the following dataframe in pandas:

>>>name   food  beverage  age
0  Ruth   Burger Cola      23
1 Dina    Pasta  water     19
2 Joel    Tuna   water     28
3 Daniel  null   soda      30
4 Tomas   null   cola     10

I want to put condistion that if value in food column is null, the age and beverage will change into ' ' (blank as well),

I have wrote this code for that:

if df[(df['food'].isna())]:
  df['beverage']=' '
  df['age']=' '

but I keep getting error:

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

I have playes with the location of the ([ but didn't help, what do I do wrong?

Aucun commentaire:

Enregistrer un commentaire