Python:内插x,y,z数据

时间:2019-04-24 20:15:48

标签: python pandas interpolation

数据

 ,1,2,3
5,10,11,12
6,13,14,15
7,16,17,18

数据在熊猫df中

1,2,3是x坐标(列标题) 5,6,7是y坐标(索引)

10-18是z坐标

我需要插值例如x = 2.5和y = 6.3

1 个答案:

答案 0 :(得分:0)

让我们使用reindexinterpolate

#read dataframe in 
df = pd.read_clipboard(sep=',', index_col=[0])

#change dtype of column headers to floats
df.columns= df.columns.astype('float')

#Use reindex and interpolate to fill values along each axis
df_out = df.reindex([5,6,6.3,7]).interpolate(method='index', axis=0)\
           .reindex([1,2,2.5,3],axis=1).interpolate(method='index', axis=1)

#Output resulting dataframe
df_out

输出:

      1.0   2.0   2.5   3.0

5.0  10.0  11.0  11.5  12.0
6.0  13.0  14.0  14.5  15.0
6.3  13.9  14.9  15.4  15.9
7.0  16.0  17.0  17.5  18.0

然后

df_out.loc[6.3, 2.5]

输出:

15.399999999999999