3
votes

My code looks like that:

if df['FLAG'] == 1:
    df['VAL'] = df['VAL'].fillna(median)
elif df['FLAG'] == 0:
    df['VAL'] = df['VAL'].fillna(0)

Which returns - The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I have tried with doing like a mask and then applying it with a.all() but it didn't worked out. I'd be very thankful for enlightment!

Edit: I've solution for my problem right here - Link

2

2 Answers

4
votes

This is an elementwise operation, and you can vectorize this. Build an array with np.where and pass that to fillna.

df['VAL'] = df['VAL'].fillna(np.where(df['FLAG'], median, 0))
1
votes

You may can do this

 df.loc[df['VAL'].isna(),'Val']=df['FLAG']*median