【问题标题】:Python:Interpolate x, y, z dataPython:插值 x、y、z 数据
【发布时间】:2019-09-14 05:08:43
【问题描述】:

数据

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

数据在 pandas df 中

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

10-18 是 z 坐标

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

【问题讨论】:

    标签: python pandas interpolation


    【解决方案1】:

    让我们使用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      • 2018-06-02
      • 1970-01-01
      • 2015-04-26
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      相关资源
      最近更新 更多