Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 插值_Python_Pandas_Interpolation - Fatal编程技术网

Python 插值

Python 插值,python,pandas,interpolation,Python,Pandas,Interpolation,我知道这个主题在堆栈溢出问题上被提过几次,但是我仍然遇到了一个插值问题 我有一个由一组列组成的复杂数据框架,如果简化,它可能看起来像这样: df_new = pd.DataFrame(np.random.randn(5,7), columns=[402.3, 407.2, 412.3, 415.8, 419.9, 423.5, 428.3]) wl = np.array([400.0, 408.2, 412.5, 417.2, 420.5, 423.3, 425.0]) 所以我需要做的

我知道这个主题在堆栈溢出问题上被提过几次,但是我仍然遇到了一个插值问题

我有一个由一组列组成的复杂数据框架,如果简化,它可能看起来像这样:

df_new = pd.DataFrame(np.random.randn(5,7), columns=[402.3, 407.2, 412.3, 415.8, 419.9, 423.5, 428.3])
wl     = np.array([400.0, 408.2, 412.5, 417.2, 420.5, 423.3, 425.0])
所以我需要做的是对每一行按列插入新指定的cols(wl)值


以及如何获取新的数据帧,其中列仅包含wl数组中显示的值?

使用
reindex
wl
作为新列(其值将用NaN填充)。 然后使用
插值(轴=1)
跨列插值。 严格来说,插值只在已知值之间进行。 但是,您可以使用
limit\u direction='both'
在前进和后退方向上填充NaN边值:

>>> df_new.reindex(columns=df_new.columns.union(wl)).interpolate(axis=1, limit_direction='both')
      400.0     402.3     407.2     408.2     412.3     412.5     415.8     417.2     419.9     420.5     423.3     423.5     425.0     428.3
0  0.342346  0.342346  1.502418  1.102496  0.702573  0.379089  0.055606 -0.135563 -0.326732 -0.022298  0.282135  0.586569  0.164917 -0.256734
1 -0.220773 -0.220773 -0.567199 -0.789194 -1.011190 -0.485832  0.039526 -0.426771 -0.893069 -0.191818  0.509432  1.210683  0.414023 -0.382636
2  0.078147  0.078147  0.335040 -0.146892 -0.628824 -0.280976  0.066873 -0.881153 -1.829178 -0.960608 -0.092038  0.776532  0.458758  0.140985
3 -0.792214 -0.792214  0.254805  0.027573 -0.199659 -1.173250 -2.146841 -1.421482 -0.696124 -0.073018  0.550088  1.173194 -0.049967 -1.273128
4 -0.485818 -0.485818  0.019046 -1.421351 -2.861747 -1.020571  0.820605  0.097722 -0.625160 -0.782700 -0.940241 -1.097781 -0.809617 -0.521453
请注意,数据帧在数据结构中存储值。因此,按列计算通常比按行计算更有效。因此,最好转置数据帧:

df = df_new.T
然后按照上述步骤进行类似操作:

df = df.reindex(index=df.index.union(wl))
df = df.interpolate(limit_direction='both')

如果要外推边值,可以使用:
fill\u value='extraction'

import numpy as np
import pandas as pd
import scipy.interpolate as interpolate
np.random.seed(2018)

df_new = pd.DataFrame(np.random.randn(5,7), columns=[402.3, 407.2, 412.3, 415.8, 419.9, 423.5, 428.3])
wl = np.array([400.0, 408.2, 412.5, 417.2, 420.5, 423.3, 425.0, 500])

x = df_new.columns
y = df_new.values
newx = x.union(wl)
result = pd.DataFrame(
    interpolate.interp1d(x, y, fill_value='extrapolate')(newx),
    columns=newx)
屈服

      400.0     402.3     407.2     408.2     412.3     412.5     415.8     417.2     419.9     420.5     423.3     423.5     425.0     428.3      500.0
0 -0.679793 -0.276768  0.581851  0.889017  2.148399  1.952520 -1.279487 -0.671080  0.502277  0.561236  0.836376  0.856029  0.543898 -0.142790 -15.062654
1  0.484717  0.110079 -0.688065 -0.468138  0.433564  0.437944  0.510221  0.279613 -0.165131 -0.362906 -1.285854 -1.351779 -0.758526  0.546631  28.904127
2  1.303039  1.230655  1.076446  0.628001 -1.210625 -1.158971 -0.306677 -0.563028 -1.057419 -0.814173  0.320975  0.402057  0.366778  0.289165  -1.397156
3  2.385057  1.282733 -1.065696 -1.191370 -1.706633 -1.618985 -0.172797 -0.092039  0.063710  0.114863  0.353577  0.370628 -0.246613 -1.604543 -31.108665
4 -3.360837 -2.165729  0.380370  0.251572 -0.276501 -0.293597 -0.575682 -0.235060  0.421854  0.469009  0.689062  0.704780  0.498724  0.045401  -9.804075

如果希望创建仅包含
wl
列的数据帧,可以使用
result[wl]
子选择这些列,也可以仅在
wl
值处简化插值:

result_wl = pd.DataFrame(
    interpolate.interp1d(x, y, fill_value='extrapolate')(wl),
    columns=wl)

太好了,谢谢!我唯一的问题是极限方向对下限不起作用。我得到了一些价值观。。。。有什么提示说明为什么会发生这种情况吗?您的实际数据帧与此处显示的玩具数据帧之间一定存在一些差异。我很抱歉地说,我没有成功地猜测出这种差异可能是什么。但也许我们首先应该确定你真正想要的行为。您是否只想向前和向后填充边缘上的NAN?(这应该是
limit\u direction='both'
所做的),还是要外推值?如果是这样,我想我必须修改我的帖子,因为
df.interpolate
不会这么做。相反,我认为您可能需要
scipy.interpolate.interp1d(…,fill_value='extraction')
。如果您可以构造一个(希望是简单的)可运行的示例来演示这个问题,我或这里的某个人可能知道如何修复它。不知道问题可能是什么,因为在大型数据集上实现它之前,我使用了非常相同的示例。但我真的很感谢你的帮助,答案非常有用,非常感谢!你是对的,我可能会使用scipy的外推来解决这个问题。@PEBKAC:或者使用
result[wl]
子选择
wl
列,或者,如果你知道这些是你想要的唯一列,你可以通过仅在
wl
值处插值直接构造这个数据帧。我已经编辑了上面的帖子来表达我的意思。