从 pandas 多索引数据框中搜索插值数据

问题描述 投票:0回答:1

我已经建立了一个多索引 pandas 数据框,如下所示:

import pandas as pd
GEN_power_vs_PF_index = pd.MultiIndex.from_tuples([('GEN PF', '0.85'),
                                                   ('GEN PF', '0.9'),
                                                   ('GEN PF', '0.95'),
                                                   ('GEN PF', '1.0')])

GEN_power_vs_PF_columns = pd.MultiIndex.from_tuples([('Generator power at terminal, (kW)', 201875),
                                                     ('Generator power at terminal, (kW)', 403750),
                                                     ('Generator power at terminal, (kW)', 605625),
                                                     ('Generator power at terminal, (kW)', 807500),
                                                     ('Generator power at terminal, (kW)', 1009375)])

GEN_power_vs_PF = pd.DataFrame([(3217.6, 4259.1, 5847, 8024.1, 11059.1),
                                (3133.2, 4023, 5390.5, 7242.6, 9678.4),
                                (3045.4, 3791.7, 4954.4, 6538.8, 8564),
                                (2891.6, 3439.5, 4346.6, 5609.6, 7228.9)],
                               index=GEN_power_vs_PF_index,
                               columns=GEN_power_vs_PF_columns)

如何从数据帧中搜索插值,例如对于 0.9769 的“GEN PF”值和 736381.3 的“终端发电机功率,(kW)”?

我对 pandas 相当陌生,到目前为止,我已经尝试使用 df.loc 方法 - 只适合查找现成的数据(无需插值),即使这样我也无法执行 2D 搜索;不断出现关键错误

pandas multi-index
1个回答
0
投票

您可以修改此解决方案

from scipy import interpolate
x = GEN_power_vs_PF.index.get_level_values(1).astype(float)
y = GEN_power_vs_PF.columns.get_level_values(1)
z = GEN_power_vs_PF.to_numpy()
# you have to set kx and ky small for this small example dataset
# 3 is more usual and is the default
# s=0 will ensure this interpolates.  s>0 will smooth the data
# you can also specify a bounding box outside the data limits
# if you want to extrapolate
sp = interpolate.RectBivariateSpline(x, y, z, kx=2, ky=2, s=0)

print (sp([0.9769], [736381.3]))
[[5528.38273829]]
© www.soinside.com 2019 - 2024. All rights reserved.