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
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))
你可以这样做
df.loc[df['VAL'].isna(),'Val']=df['FLAG']*median
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.