Pandas - 对索引中的值进行插值

Pandas - interpolate over values in index

我有以下 Pandas 数据框:

       a0     a1      a2      a3
0.2  0.46  15.85  124.06 -380.04
0.4  0.21  28.20  -53.17   87.97
0.6  1.10  -5.55  167.76 -417.72
0.8  0.82   6.11   16.90  -70.86
1.0  1.00   0.00    0.00    0.00

制作者:

import pandas as pd
df = pd.DataFrame(data={'a0': [0.46,0.21,1.10,0.82,1],
                            'a1': [15.85,28.20,-5.55,6.11,0],
                            'a2': [124.06,-53.17,167.76,16.90,0],
                            'a3': [-380.04,87.97,-417.72,-70.86,0]},
                      index=pd.Series(['0.2', '0.4', '0.6','0.8','1.0']))

a0,a1,a2,a3 是拟合 y= a0 + a1x + a2x^2 + a3*x^3.[=14= 的多项式系数]

已针对 5 个比率进行了 5 次拟合 Ht/H,这些比率在指数中。

我想 return a0.. a3 的指定 Ht/H 比率值。

比如我指定Ht/H=0.9,我想得到a0= 0.91, a1= 3.05,a2= 8.45,a3= -35.43.

首先我注意到您的索引当前是字符串,并且您需要数字进行插值。也一样:

df.index = pd.to_numeric(df.index)

让我们试试reindex:

s = 0.9

# create new index that includes the new value
new_idx = np.unique(list(df.index) + [s])

df.reindex(new_idx).interpolate('index').loc[s]

输出:

a0     0.910
a1     3.055
a2     8.450
a3   -35.430
Name: 0.9, dtype: float64