Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 348
Reut Avatar asked Sep 13 '20 15:09

Reut


People also ask

How do you replace values in a DataFrame based on a condition?

You can replace values of all or selected columns based on the condition of pandas DataFrame by using DataFrame. loc[ ] property. The loc[] is used to access a group of rows and columns by label(s) or a boolean array. It can access and can also manipulate the values of pandas DataFrame.

How do you change a particular cell value in a DataFrame?

You can set cell value of pandas dataframe using df. iat[row_index, column_index] = 'Cell Value'. Dataframe at property of the dataframe allows you to access the single value of the row/column pair using the row and column indexes. It accepts two parameters.


3 Answers

You can use boolean indexing to assign the values based on the condition:

df.loc[df['food'].isna(), ['age', 'beverage']] = ''

     name    food beverage age
0    Ruth  Burger     Cola  23
1    Dina   Pasta    water  19
2    Joel    Tuna    water  28
3  Daniel     NaN             
4   Tomas     NaN             
like image 154
Shubham Sharma Avatar answered Oct 16 '22 09:10

Shubham Sharma


Try with mask

df[['beverage','age']] = df[['beverage','age']].mask(df['food'].isna(),'')

df
Out[86]: 
     name    food beverage age
0    Ruth  Burger     Cola  23
1    Dina   Pasta    water  19
2    Joel    Tuna    water  28
3  Daniel     NaN             
4   Tomas     NaN             
like image 28
BENY Avatar answered Oct 16 '22 11:10

BENY


You can use np.where:

cols = ['beverage','age']
arr = np.where(df['food'].isna()[:,None],'',df[cols])
#for NaN : arr = np.where(df['food'].isna()[:,None],np.nan,df[cols])
df[cols] = arr

     name    food beverage age
0    Ruth  Burger     Cola  23
1    Dina   Pasta    water  19
2    Joel    Tuna    water  28
3  Daniel     NaN             
4   Tomas     NaN             
like image 4
anky Avatar answered Oct 16 '22 11:10

anky