两组点之间的插值

interpolation between two groups of points

我有一个具有以下形式的数据集:

     A    B     C     D     E
0  0.5  0.2  0.25  0.75  1.25
1  0.5  0.3  0.12  0.41  1.40
2  0.5  0.4  0.85  0.15  1.55
3  1.0  0.2  0.11  0.15  1.25
4  1.0  0.3  0.10  0.11  1.40
5  1.0  0.4  0.87  0.14  1.25
6  2.0  0.2  0.23  0.45  1.55
7  2.0  0.3  0.74  0.85  1.25
8  2.0  0.4  0.55  0.55  1.40

下面是使用 pandas:

生成此 DataFrame 的代码
import pandas as pd

data = [[0.5, 0.2, 0.25, 0.75, 1.25],
        [0.5, 0.3, 0.12, 0.41, 1.40],
        [0.5, 0.4, 0.85, 0.15, 1.55],
        [1.0, 0.2, 0.11, 0.15, 1.25],
        [1.0, 0.3, 0.10, 0.11, 1.40],
        [1.0, 0.4, 0.87, 0.14, 1.25],
        [2.0, 0.2, 0.23, 0.45, 1.55],
        [2.0, 0.3, 0.74, 0.85, 1.25],
        [2.0, 0.4, 0.55, 0.55, 1.40]]

df = pd.DataFrame(data,columns=['A','B','C','D','E'])

此数据表示实验结果,其中每个 A B 和 E 都有一个唯一值 C

我想要的是执行线性插值,以便根据 A=0.5 和 A = 1 的值获得 A= 0.7 的类似数据。 预期的输出应该是这样的:

     A    B     C     D     E
0  0.5  0.2  0.25  0.75  1.25
1  0.5  0.3  0.12  0.41  1.40
2  0.5  0.4  0.85  0.15  1.55
3  0.7  0.2  xxx   xxx   1.25
4  0.7  0.3  xxx   xxx   1.40
5  0.7  0.4  xxx   xxx   1.55
6  1.0  0.2  0.11  0.15  1.25
7  1.0  0.3  0.10  0.11  1.40
8  1.0  0.4  0.87  0.14  1.25
9  2.0  0.2  0.23  0.45  1.55
10  2.0  0.3  0.74  0.85  1.25
11  2.0  0.4  0.55  0.55  1.40

在 Python 中是否有直接的方法来做到这一点?我尝试使用 the panda interpolate 但我得到的值没有意义。

有什么建议吗?

下面是一个示例,说明如何创建一个插值函数,将 A 列的值映射到 C 列的值(为 A 的值任意选择 0.5 到 2.0):

import pandas as pd
import numpy as np
from scipy import interpolate

# Set up the dataframe
data = [[0.5, 0.2, 0.25, 0.75, 1.25],
        [0.5, 0.3, 0.12, 0.41, 1.40],
        [0.5, 0.4, 0.85, 0.15, 1.55],
        [1.0, 0.2, 0.11, 0.15, 1.25],
        [1.0, 0.3, 0.10, 0.11, 1.40],
        [1.0, 0.4, 0.87, 0.14, 1.25],
        [2.0, 0.2, 0.23, 0.45, 1.55],
        [2.0, 0.3, 0.74, 0.85, 1.25],
        [2.0, 0.4, 0.55, 0.55, 1.40]]
df = pd.DataFrame(data,columns=['A','B','C','D','E'])

# Create the interpolation function
f = interpolate.interp1d(df['A'], df['C'])

# Evaluate new A (x) values to get new C (y) values via interpolation
xnew = np.linspace(0.5, 2.0, 10)
ynew = f(xnew)
print("%-7s %-7s"%("A","C"))
print("-"*16)
for x, y in zip(xnew, ynew):
    print("%0.4f\t%0.4f"%(x,y))

结果:

A       C
----------------
0.5000  0.8500
0.6667  0.6033
0.8333  0.3567
1.0000  0.8700
1.1667  0.7633
1.3333  0.6567
1.5000  0.5500
1.6667  0.4433
1.8333  0.3367
2.0000  0.5500