Pandas 缺失值:填充最接近的非 NaN 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44782881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:53:29  来源:igfitidea点击:

Pandas missing values : fill with the closest non NaN value

pythonpandastime-seriesnanfillna

提问by Clément F

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

假设我有一个包含多个连续 NaN 的 Pandas 系列。我知道fillna有几种方法可以填充缺失值(backfillfill forward),但我想用最接近的非 NaN 值填充它们。这是我所拥有的一个例子:

`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])

和我想要的一个例子: s = pd.Series([0, 1, 1, 1, 3, 3, 3])

Does anyone know I could do that?

有谁知道我能做到吗?

Thanks!

谢谢!

回答by DSM

You could use Series.interpolatewith method='nearest':

你可以使用Series.interpolatemethod='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