0

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

Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
Kay Trinh
  • 3
  • 1
  • Does this answer your question? [How can I check for NaN values?](https://stackoverflow.com/questions/944700/how-can-i-check-for-nan-values) – Ynjxsjmh May 20 '22 at 17:02

2 Answers2

0

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()
PTQuoc
  • 938
  • 4
  • 13
0

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
Zaero Divide
  • 699
  • 2
  • 10