[英]Pandas missing values : fill with the closest non NaN value
Assume I have a pandas series with several consecutive NaNs. 假设我有一个连续几个NaN的熊猫系列。 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. 我知道
fillna
有几种填充缺失值的方法( backfill
和fill forward
),但我想用最接近的非NaN值填充它们。 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])
以及我想要的一个例子:
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'
: 您可以将
Series.interpolate
与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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.