Python 3.x 熊猫-对索引中的值进行插值
我有以下数据帧:Python 3.x 熊猫-对索引中的值进行插值,python-3.x,pandas,dataframe,interpolation,Python 3.x,Pandas,Dataframe,Interpolation,我有以下数据帧: 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.DataF
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的多项式系数
对5个比率Ht/H进行了5次拟合,这些比率在指数上
我想返回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)
让我们尝试重新编制索引:
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