Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace NaNs by average of preceding and succeeding values in pandas DataFrame?

If I have some missing values and I would like to replace all NaN with average of preceding and succeeding values, how can I do that ?.

I know I can use pandas.DataFrame.fillna with method='ffill' or method='bfill' options to replace the NaN values by preceding or succeeding values, however I would like to apply the average of those values on the dataframe instead of iterating over rows and columns.

like image 650
Yasmin Avatar asked Dec 05 '25 10:12

Yasmin


1 Answers

Try DataFrame.interpolate(). Example from the panda docs:

In [65]: df = pd.DataFrame({'A': [1, 2.1, np.nan, 4.7, 5.6, 6.8],
   ....:                    'B': [.25, np.nan, np.nan, 4, 12.2, 14.4]})
   ....: 

In [66]: df
Out[66]: 
     A      B
0  1.0   0.25
1  2.1    NaN
2  NaN    NaN
3  4.7   4.00
4  5.6  12.20
5  6.8  14.40

In [67]: df.interpolate()
Out[67]: 
     A      B
0  1.0   0.25
1  2.1   1.50
2  3.4   2.75
3  4.7   4.00
4  5.6  12.20
5  6.8  14.40
like image 71
RootTwo Avatar answered Dec 08 '25 01:12

RootTwo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!