简体   繁体   中英

Pandas missing values : fill with the closest non NaN value

Assume I have a pandas series with several consecutive NaNs. I know fillna has several methods to fill missing values ( backfill and fill forward ), but I want to fill them with the closest non NaN value. Here's an example of what I have:

`s = pd.Series([0, 1, np.nan, np.nan, np.nan, np.nan, 3])`

And an example of what I want: s = pd.Series([0, 1, 1, 1, 3, 3, 3])

Does anyone know I could do that?

Thanks!

You could use Series.interpolate with method='nearest' :

In [11]: s = pd.Series([0, 1, np.nan, np.nan, np.nan, np.nan, 3])

In [12]: s.interpolate(method='nearest')
Out[12]: 
0    0.0
1    1.0
2    1.0
3    1.0
4    3.0
5    3.0
6    3.0
dtype: float64

In [13]: s = pd.Series([0, 1, np.nan, np.nan, 2, np.nan, np.nan, 3])

In [14]: s.interpolate(method='nearest')
Out[14]: 
0    0.0
1    1.0
2    1.0
3    2.0
4    2.0
5    2.0
6    3.0
7    3.0
dtype: float64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM