【问题标题】:In python pandas, how can I re-sample and interpolate a DataFrame?在 python pandas 中,如何重新采样和插入 DataFrame?
【发布时间】:2017-05-20 11:35:46
【问题描述】:

我有一个 pd DataFrame,通常采用这种格式:

   1       2          3          4  
0.1100 0.0000E+00 1.0000E+00 5.0000E+00  
0.1323 7.7444E-05 8.7935E-01 1.0452E+00  
0.1545 4.3548E-04 7.7209E-01 4.5432E-01  
0.1768 1.2130E-03 6.7193E-01 2.6896E-01  
0.1990 2.5349E-03 5.7904E-01 1.8439E-01  
0.2213 4.5260E-03 4.9407E-01 1.3771E-01 

我想做的是从列表中重新采样第 1 列(索引)值,例如:

indexList = numpy.linspace(0.11, 0.25, 8)

然后我需要从输入 DataFrame 线性插值第 2、3 和 4 列的值(我重新采样/重新索引的总是只有我的第 1 列) - 并且如果需要外推,作为最小值/最大值我的列表的值不一定在我现有的第 1 列(索引)中。然而,关键点是插值部分。我对 python 很陌生,但我正在考虑使用这样的方法:

  1. output_df = DataFrame.reindex(index=indexList) - 这将主要为我提供第 2-4 列的 NaN。
  2. 对于索引,output_df.iterrows() 中的行
    “计算内插/外插值的函数 DataFrame 并将它们插入正确的行/列"

感觉我应该能够使用 .interpolate 功能,但我不知道如何使用。我不能直接使用它——这太不准确了,因为我在第 2-4 列中提到的重新索引后的大部分条目都是 NaN;插值应该在我的初始 DataFrame 的两个最接近的值内完成。有什么好的建议吗? (如果我的格式/意图不清楚,请告诉我...)

【问题讨论】:

    标签: python pandas interpolation reindex


    【解决方案1】:

    假设第 1 列在索引中,您可以使用原始值以及您创建的列表重新索引您的数据框,然后使用 interpolate 填充 nan。

    df1 = df.reindex(df.index.union(np.linspace(.11,.25,8)))
    df1.interpolate('index')
    
                   2         3         4
    0.1100  0.000000  1.000000  5.000000
    0.1300  0.000069  0.891794  1.453094
    0.1323  0.000077  0.879350  1.045200
    0.1500  0.000363  0.793832  0.574093
    0.1545  0.000435  0.772090  0.454320
    0.1700  0.000976  0.702472  0.325482
    0.1768  0.001213  0.671930  0.268960
    0.1900  0.001999  0.616698  0.218675
    0.1990  0.002535  0.579040  0.184390
    0.2100  0.003517  0.537127  0.161364
    0.2213  0.004526  0.494070  0.137710
    0.2300  0.004526  0.494070  0.137710
    0.2500  0.004526  0.494070  0.137710
    

    【讨论】:

    • 谢谢,这真的解决了我的问题:) .union 可以解决问题,因为它使插值更加准确。我的目标是最后只有我的列表的新索引值,所以我在最后添加了 'df1 = df1.reindex(index=indexList) 以摆脱我的初始索引。
    【解决方案2】:

    在我们开始一些咒语之前:

    import pandas as pd
    import numpy
    
    LENGTH=8
    

    让我们从加载您的数据开始(我们将更改为 csv,因为它更容易):

    x="""   1       2          3          4
    0.1100 0.0000E+00 1.0000E+00 5.0000E+00
    0.1323 7.7444E-05 8.7935E-01 1.0452E+00
    0.1545 4.3548E-04 7.7209E-01 4.5432E-01
    0.1768 1.2130E-03 6.7193E-01 2.6896E-01
    0.1990 2.5349E-03 5.7904E-01 1.8439E-01
    0.2213 4.5260E-03 4.9407E-01 1.3771E-01
    """
    nx = ""
    for l in x.split('\n'):
        nx += ','.join(l.split()) + '\n'
    df= pd.read_csv(pd.compat.StringIO(nx))
    

    现在,您希望在相同数据上插值一个新的数据框,但数组包含 0.11 和 0.25 之间的 8 个值:

    indexList = numpy.linspace(0.11, 0.25, LENGTH)
    

    我们将使用第一列作为索引,并重新索引:

    df_interpolated = df.reindex(df.index.union(indexList)).interpolate('index')
    df_interpolated.head(LENGTH)
    
                 1         2         3         4
    0.00  0.110000  0.000000  1.000000  5.000000
    0.11  0.112453  0.000009  0.986729  4.564972
    0.13  0.112899  0.000010  0.984316  4.485876
    0.15  0.113345  0.000012  0.981903  4.406780
    0.17  0.113791  0.000013  0.979490  4.327684
    0.19  0.114237  0.000015  0.977077  4.248588
    0.21  0.114683  0.000016  0.974664  4.169492
    0.23  0.115129  0.000018  0.972251  4.090396
    0.25  0.115575  0.000019  0.969838  4.011300
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-25
      • 2013-12-06
      • 2020-11-06
      • 1970-01-01
      • 2012-08-24
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多