Python Pandas使用新的x轴进行插值 [英] Python Pandas interpolate with new x-axis

查看:178
本文介绍了Python Pandas使用新的x轴进行插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对以下结构的熊猫系列进行插值

I want to do interpolation for a Pandas series of the following structure

X
22.88      3.047
45.75      3.215
68.63      3.328
91.50      3.423
114.38     3.516
137.25     3.578
163.40     3.676
196.08     3.756
228.76     3.861
261.44     3.942
294.12     4.012
326.80     4.084
359.48     4.147
392.16     4.197
Name: Y, dtype: float64

我想对数据进行插值,以便有一个新的系列来覆盖X=[23:392:1].我查找了文档,但没有找到我可以在其中输入新x轴的位置.我错过了什么?如何使用新的x轴进行插值?

I want to interpolate the data so that I have a new series to cover X=[23:392:1]. I looked up the document but didn't find where I could input the new x-axis. Did I miss something? How can I do interpolation with the new x-axis?

推荐答案

这可以通过pandasreindexinterpolate完成:

In [27]: s
Out[27]: 
            1
0            
22.88   3.047
45.75   3.215
68.63   3.328
91.50   3.423
114.38  3.516
137.25  3.578
163.40  3.676
196.08  3.756
228.76  3.861
261.44  3.942
294.12  4.012
326.80  4.084
359.48  4.147
392.16  4.197

[14 rows x 1 columns]

In [28]: idx = pd.Index(np.arange(23, 392))

In [29]: s.reindex(s.index + idx).interpolate(method='values')
Out[29]: 
              1
22.88  3.047000
23.00  3.047882
24.00  3.055227
25.00  3.062573
26.00  3.069919
27.00  3.077265
28.00  3.084611
29.00  3.091957
30.00  3.099303
31.00  3.106648
32.00  3.113994
33.00  3.121340
34.00  3.128686
35.00  3.136032
36.00  3.143378
37.00  3.150724
38.00  3.158070
39.00  3.165415
40.00  3.172761
41.00  3.180107
42.00  3.187453
43.00  3.194799
44.00  3.202145
45.00  3.209491
45.75  3.215000
46.00  3.216235
47.00  3.221174
48.00  3.226112

这个想法是创建您想要的索引(s.index + idx),该索引会自动排序,为该索引重新索引(在新点上生成一堆NaN,然后进行插值以填充NaN s,使用values方法在索引点处进行插值.

The idea is the create the index you want (s.index + idx), which is sorted automatically, reindex an that (which makes a bunch of NaNs at the new points, and the interpolate to fill the NaNs, using the values method, which interpolates at the index points.

这篇关于Python Pandas使用新的x轴进行插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆