Home > Back-end >  how to replace NaN using lambda
how to replace NaN using lambda

Time:05-21

I have dataframe like this:

dict={'priorSaleYear':[2004, np.NaN],'lastSaleYear':[2008, 2009]}
df=pd.DataFrame(dict, index=[1,2])

I want to replace the np.nan with the lastSaleYear minor a number:

df['priorSaleYear']= df.apply (lambda row: (row['lastSaleYear'] - b) if row['priorSaleYear'] is np.nan else row['priorSaleYear'], axis=1)

But it seems like row['priorSaleYear'] is np.nan not work, can someone help me, thanks

CodePudding user response:

You did not clarify if there are more than one continuous NaN how the fill should be. Here, I assume that for all NaN, they will use the most recent available value.

df['priorSaleYear'] = df['priorSaleYear'].ffill()

CodePudding user response:

Use fillna() and state with what you want to fill it:

df['priorSaleYear']=df.priorSaleYear.fillna(df.lastSaleYear-1337)

>>> df
   priorSaleYear  lastSaleYear
1         2004.0          2008
2          672.0          2009
  • Related