插入多索引 a pandas 数据帧

Interpolating multi index a pandas dataframe

我需要插入多索引数据帧:

例如:

这是主数据框:

a    b    c    result
1    1    1    6
1    1    2    9
1    2    1    8
1    2    2    11
2    1    1    7
2    1    2    10
2    2    1    9
2    2    2    12

我需要找到结果:

1.3    1.7    1.55    

到目前为止我一直在做的是在里面添加一个 pd.Series 和 NaN 分别针对每个索引。

如你所见。这似乎是一种非常低效的方式。

如果有人能丰富我,我会很高兴。

P.S。 我花了一些时间查看 SO,如果答案在那里,我错过了它:

Fill multi-index Pandas DataFrame with interpolation

Resampling Within a Pandas MultiIndex

pandas multiindex dataframe, ND interpolation for missing values

Fill multi-index Pandas DataFrame with interpolation

算法:

第 1 阶段:

a    b    c    result
1    1    1    6
1    1    2    9
1    2    1    8
1    2    2    11
1.3    1    1    6.3
1.3    1    2    9.3
1.3    2    1    8.3
1.3    2    2    11.3
2    1    1    7
2    1    2    10
2    2    1    9
2    2    2    12

第 2 阶段:

a    b    c    result
1    1    1    6
1    1    2    9
1    2    1    8
1    2    2    11
1.3    1    1    6.3
1.3    1    2    9.3
1.3    1.7    1    7.7
1.3    1.7    2    10.7
1.3    2    1    8.3
1.3    2    2    11.3
2    1    1    7
2    1    2    10
2    2    1    9
2    2    2    12

第 3 阶段:

a    b    c    result
1    1    1    6
1    1    2    9
1    2    1    8
1    2    2    11
1.3    1    1    6.3
1.3    1    2    9.3
1.3    1.7    1    7.7
1.3    1.7    1.55    9.35
1.3    1.7    2    10.7
1.3    2    1    8.3
1.3    2    2    11.3
2    1    1    7
2    1    2    10
2    2    1    9
2    2    2    12

你可以使用scipy.interpolate.LinearNDInterpolator做你想做的事。如果数据框是具有列 'a'、'b' 和 'c' 的 MultiIndex,则:

from scipy.interpolate import LinearNDInterpolator as lNDI
print (lNDI(points=df.index.to_frame().values, values=df.result.values)([1.3, 1.7, 1.55]))

现在如果你有数据帧,所有元组(a,b,c)作为你想要计算的索引,你可以这样做:

def pd_interpolate_MI (df_input, df_toInterpolate):
    from scipy.interpolate import LinearNDInterpolator as lNDI
    #create the function of interpolation
    func_interp = lNDI(points=df_input.index.to_frame().values, values=df_input.result.values)
    #calculate the value for the unknown index
    df_toInterpolate['result'] = func_interp(df_toInterpolate.index.to_frame().values)
    #return the dataframe with the new values
    return pd.concat([df_input, df_toInterpolate]).sort_index()

然后以您的 dfdf_toI = pd.DataFrame(index=pd.MultiIndex.from_tuples([(1.3, 1.7, 1.55),(1.7, 1.4, 1.9)],names=df.index.names)) 为例 然后你得到

print (pd_interpolate_MI(df, df_toI))
              result
a   b   c           
1.0 1.0 1.00    6.00
        2.00    9.00
    2.0 1.00    8.00
        2.00   11.00
1.3 1.7 1.55    9.35
1.7 1.4 1.90   10.20
2.0 1.0 1.00    7.00
        2.00   10.00
    2.0 1.00    9.00
        2.00   12.00