如何从 Python table 获取将在列和行之间插入的值?
How to get value from Python table that will be interpolate between columns and rows?
我在 Pandas 中创建了 table (DataFrame)。它是 2D table,整数作为列索引,整数作为行索引(即 position x
和 position y
)。
我知道如何使用索引获取 table 的 "cell" 中的值,但我想获取值 "from between" 将被线性插值的列和行。
最好,我想对保存在两个 tables Position_x(m x n)
、Position_y(m x n)
中的大量 x、y 执行此操作,并将结果放入 table Results(m x n)
https://i.stack.imgur.com/utv03.png
以下是 Excel 中此类过程的示例:
https://superuser.com/questions/625154/what-is-the-simplest-way-to-interpolate-and-lookup-in-an-x-y-table-in-excel
谢谢
西蒙
如果我理解你的问题:
您可以先使用 pandas.melt 将多列结果 table 转换为单列结果 table。
然后,可以用插值
希望我有所帮助。
我找到了 90% 有效的方法,但是它有两个缺点:
1)索引和列需要严格递增,
2) 对于一组 n 个输入对,它绘制 n x n 结果数组而不是仅仅 n 个结果(例如下面的 3 对输入点我只需要 3 个结果值,使用该代码我将得到 9 个值作为输入点的所有组合).
这是我的发现:
import scipy
import scipy.interpolate
import numpy as np
import pandas as pd
x=np.array([0,10,25,60,100]) #Index
y=np.array([1000,1200,1400,1600]) #Column
data=np.array([[60,54,33,0],
[50,46,10,0],
[42,32,5,0],
[30,30,2,0],
[10,10,0,0]])
Table_to_Interpolate=pd.DataFrame(data,index=x,columns=y)
sp=scipy.interpolate.RectBivariateSpline(x,y,data, kx=1, ky=1, s=0)
scipy.interpolate.RectBivariateSpline(x,y,data, kx=1, ky=1, s=0)
Input_Xs=12, 44, 69
Input_Ys=1150, 1326, 1416
Results=pd.DataFrame(sp(Input_Xs, Input_Ys), index=Input_Xs, columns=Input_Ys,)
它并不完美,但它是我能找到的最好的。
我在 Pandas 中创建了 table (DataFrame)。它是 2D table,整数作为列索引,整数作为行索引(即 position x
和 position y
)。
我知道如何使用索引获取 table 的 "cell" 中的值,但我想获取值 "from between" 将被线性插值的列和行。
最好,我想对保存在两个 tables Position_x(m x n)
、Position_y(m x n)
中的大量 x、y 执行此操作,并将结果放入 table Results(m x n)
https://i.stack.imgur.com/utv03.png
以下是 Excel 中此类过程的示例: https://superuser.com/questions/625154/what-is-the-simplest-way-to-interpolate-and-lookup-in-an-x-y-table-in-excel
谢谢 西蒙
如果我理解你的问题:
您可以先使用 pandas.melt 将多列结果 table 转换为单列结果 table。
然后,可以用
希望我有所帮助。
我找到了 90% 有效的方法,但是它有两个缺点: 1)索引和列需要严格递增, 2) 对于一组 n 个输入对,它绘制 n x n 结果数组而不是仅仅 n 个结果(例如下面的 3 对输入点我只需要 3 个结果值,使用该代码我将得到 9 个值作为输入点的所有组合).
这是我的发现:
import scipy
import scipy.interpolate
import numpy as np
import pandas as pd
x=np.array([0,10,25,60,100]) #Index
y=np.array([1000,1200,1400,1600]) #Column
data=np.array([[60,54,33,0],
[50,46,10,0],
[42,32,5,0],
[30,30,2,0],
[10,10,0,0]])
Table_to_Interpolate=pd.DataFrame(data,index=x,columns=y)
sp=scipy.interpolate.RectBivariateSpline(x,y,data, kx=1, ky=1, s=0)
scipy.interpolate.RectBivariateSpline(x,y,data, kx=1, ky=1, s=0)
Input_Xs=12, 44, 69
Input_Ys=1150, 1326, 1416
Results=pd.DataFrame(sp(Input_Xs, Input_Ys), index=Input_Xs, columns=Input_Ys,)
它并不完美,但它是我能找到的最好的。