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?
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.
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.
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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With