'how to replace NaN using lambda

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



Solution 1:[1]

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()

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PTQuoc
Solution 2 Zaero Divide