有没有办法用 pandas 进行二维插值?

beg*_*ing 4 python math numpy scipy pandas

我的数据框看起来像:

X   Y   Val
10  10  17
10  30  28
10  40  35
10  50  15
20  10  17
20  30  18
20  40  33
20  50  15
40  10  37
40  30  29
40  40  49
40  50  40
Run Code Online (Sandbox Code Playgroud)

"Val"我想提取给定的插值"X""Y"即使它们不在 df 中。
例如:我可以很容易地得到"Val"forX=10Y=50

但是,假设我想要X=15哪个Y=15不在数据框中?是否可以提取相同的内容?Pandas有这样的功能吗?

Zep*_*hyr 5

我不知道熊猫,但你可以通过以下方式做到这一点scipy.interpolate.interp2d

f = interp2d(x = df['X'], y = df['Y'], z = df['Val'])
Run Code Online (Sandbox Code Playgroud)

然后您可以检查给定点处函数的值(x, y)

>>> f(15, 15)
array([17.97919182])
Run Code Online (Sandbox Code Playgroud)