gpt4 book ai didi

python - Pandas 在数据帧中向后插值()

转载 作者:行者123 更新时间:2023-12-02 06:31:31 25 4
gpt4 key购买 nike

展望 future ,interpolate效果很好:

       name    days
0 a NaN
1 a NaN
2 a 2
3 a 3
4 a NaN
5 a NaN

records.loc[:, 'days'].interpolate(method='linear', inplace=True)

name days
0 a NaN
1 a NaN
2 a 2
3 a 3
4 a 4
5 a 5

...但是,它不寻址开始行(仅向前)。 limit_direction 参数允许 {‘forward’, ‘backward’, ‘both’}。这些都不起作用。有没有正确的向后插值方法?

我们可以假设一系列递增或递减 1,它可能不会像本例中那样从 0 开始。

最佳答案

它似乎仅适用于参数limit,请参阅 docs [In 47] :

Add a limit_direction keyword argument that works with limit to enable interpolate to fill NaN values forward, backward, or both (GH9218, GH10420, GH11115)

records = pd.DataFrame(
{'name': {0: 'a', 1: 'a', 2: 'a', 3: 'a', 4: 'a', 5: 'a', 6: 'a', 7: 'a', 8: 'a', 9: 'a'},
'days': {0: 0.0, 1: np.nan, 2: np.nan, 3: np.nan, 4: 4.0, 5: 5.0, 6: np.nan, 7: np.nan, 8: np.nan, 9: 9.0}},
columns=['name','days'])

print (records)
name days
0 a 0.0
1 a NaN
2 a NaN
3 a NaN
4 a 4.0
5 a 5.0
6 a NaN
7 a NaN
8 a NaN
9 a 9.0
#by default limit_direction='forward'
records['forw'] = records['days'].interpolate(method='linear',
limit=1)
records['backw'] = records['days'].interpolate(method='linear',
limit_direction='backward',
limit=1)
records['both'] = records['days'].interpolate(method='linear',
limit_direction='both',
limit=1)
print (records)
name days forw backw both
0 a 0.0 0.0 0.0 0.0
1 a NaN 1.0 NaN 1.0
2 a NaN NaN NaN NaN
3 a NaN NaN 3.0 3.0
4 a 4.0 4.0 4.0 4.0
5 a 5.0 5.0 5.0 5.0
6 a NaN 6.0 NaN 6.0
7 a NaN NaN NaN NaN
8 a NaN NaN 8.0 8.0
9 a 9.0 9.0 9.0 9.0

关于python - Pandas 在数据帧中向后插值(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40582640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com