Pandas / Python:根据为一个参考列指定的值对多个列进行插值

时间:2019-01-23 06:12:43

标签: python pandas interpolation

df
Out[1]: 
             PRES   HGHT  TEMP  DWPT  RELH   MIXR  DRCT  SKNT   THTA   THTE   THTV
        0   978.0    345  17.0  16.5    97  12.22     0     0  292.0  326.8  294.1
        1   977.0    354  17.8  16.7    93  12.39     1     0  292.9  328.3  295.1
        2   970.0    416  23.4  15.4    61  11.47     4     2  299.1  332.9  301.2
        3   963.0    479  24.0  14.0    54  10.54     8     3  300.4  331.6  302.3
        4   948.7    610  23.0  13.4    55  10.28    15     6  300.7  331.2  302.5
        5   925.0    830  21.4  12.4    56   9.87    20     5  301.2  330.6  303.0
        6   916.0    914  20.7  11.7    56   9.51    20     4  301.3  329.7  303.0
        7   884.0   1219  18.2   9.2    56   8.31    60     4  301.8  326.7  303.3
        8   853.1   1524  15.7   6.7    55   7.24    35     3  302.2  324.1  303.5
        9   850.0   1555  15.4   6.4    55   7.14    20     2  302.3  323.9  303.6
        10  822.8   1829  13.3   5.6    60   6.98   300     4  302.9  324.0  304.1

如何将所有列的值插值到指定的PRES(压力)值,例如PRES = [950,900,875]?有没有一种优雅的熊猫方式可以做到这一点?

我能想到的唯一方法是,首先为循环中每个指定的PRES值在整个行中创建空的NaN值,然后将PRES设置为索引,然后使用pandas本机插值选项:

df.interpolate(method='index', inplace=True)

有没有更优雅的解决方案?

1 个答案:

答案 0 :(得分:1)

使用无循环的解决方案-通过reindexPRES列表中的union原始索引值,但仅在所有值都是唯一的情况下起作用:

PRES=[950, 900, 875]
df = df.set_index('PRES')
df = df.reindex(df.index.union(PRES)).sort_index(ascending=False).interpolate(method='index')
print (df)
         HGHT  TEMP  DWPT  RELH   MIXR   DRCT  SKNT   THTA   THTE   THTV
978.0   345.0  17.0  16.5  97.0  12.22    0.0   0.0  292.0  326.8  294.1
977.0   354.0  17.8  16.7  93.0  12.39    1.0   0.0  292.9  328.3  295.1
970.0   416.0  23.4  15.4  61.0  11.47    4.0   2.0  299.1  332.9  301.2
963.0   479.0  24.0  14.0  54.0  10.54    8.0   3.0  300.4  331.6  302.3
950.0  1829.0  13.3   5.6  60.0   6.98  300.0   4.0  302.9  324.0  304.1
948.7   610.0  23.0  13.4  55.0  10.28   15.0   6.0  300.7  331.2  302.5
925.0   830.0  21.4  12.4  56.0   9.87   20.0   5.0  301.2  330.6  303.0
916.0   914.0  20.7  11.7  56.0   9.51   20.0   4.0  301.3  329.7  303.0
900.0  1829.0  13.3   5.6  60.0   6.98  300.0   4.0  302.9  324.0  304.1
884.0  1219.0  18.2   9.2  56.0   8.31   60.0   4.0  301.8  326.7  303.3
875.0  1829.0  13.3   5.6  60.0   6.98  300.0   4.0  302.9  324.0  304.1
853.1  1524.0  15.7   6.7  55.0   7.24   35.0   3.0  302.2  324.1  303.5
850.0  1555.0  15.4   6.4  55.0   7.14   20.0   2.0  302.3  323.9  303.6
822.8  1829.0  13.3   5.6  60.0   6.98  300.0   4.0  302.9  324.0  304.1 

如果可能,PRES列中的值不是唯一的,则将concatsort_index结合使用:

PRES=[950, 900, 875]
df = df.set_index('PRES')

df = (pd.concat([df, pd.DataFrame(index=PRES)])
        .sort_index(ascending=False)
        .interpolate(method='index'))