简体   繁体   中英

Python:Interpolate x, y, z data

Data

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

the data are in a pandas df

1,2,3 are x coordinates (column headers) 5,6,7 are y coordinates (index)

10-18 are z coordinates

I need to interpolated values for example x=2.5 and y=6.3

Let's use reindex and interpolate :

#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

Output:

      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

And,

df_out.loc[6.3, 2.5]

Output:

15.399999999999999

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM