Home > Back-end >  Cannot assign "nan"/empty value in np.where
Cannot assign "nan"/empty value in np.where

Time:06-09

I'm trying to assign an empty cell (blank/nan) in my "np.where" condition to my pandas dataframe, but nothing seems to work.

The reason for this is to run fillna,ffill on the missing values.

Np.Where code:

df['x'] = np.where(df['y']>0.05,1,np.nan)

Fillna code:

df['x'] = df['x'].fillna(method="ffill")

Anybody know where I'm going wrong?

CodePudding user response:

this line of code works: df['x'] = np.where(df['y']>0.05,1,np.nan) just remove the unneeded paratheses in the right

CodePudding user response:

I was able to fix it using pandas.NA instead which fillna, for some reason, recognizes as blanks to fill with ffill

Fix:

df['x'] = np.where(df['y']>0.05,1,pd.NA)
df['x'] = df['x'].fillna(method="ffill")
  • Related