【问题标题】:Panda dataframe re-sample timeseries index with multiindex熊猫数据框使用多索引重新采样时间序列索引
【发布时间】:2018-01-27 10:32:36
【问题描述】:

我有一组数据,如何将其时间戳重新采样为 1 秒间隔,并用 0 填充数据列(“UUT”除外)。

                        UUT  Sent  Received Latency(ms)  Sum
DateTime                                                    
2018-01-25 15:03:05  uut-1     1         1         427    2
2018-01-25 15:03:05  uut-2     1         1         664    2
2018-01-25 15:03:17  uut-1     1         1         637    2
2018-01-25 15:03:17  uut-2     1         1        1229    2
2018-01-25 15:03:29  uut-1     1         1        1154    2
2018-01-25 15:03:29  uut-2     1         1        1148    2
2018-01-25 15:04:00  uut-1     1         1         279    2

输出如下:

                        UUT  Sent  Received Latency(ms)  Sum
DateTime                                                    
2018-01-25 15:03:05  uut-1     1         1         427    2
2018-01-25 15:03:05  uut-2     1         1         664    2
2018-01-25 15:03:06  uut-1     0         0           0    0
2018-01-25 15:03:06  uut-2     0         0           0    0
2018-01-25 15:03:07  uut-1     0         0           0    0
2018-01-25 15:03:07  uut-2     0         0           0    0
2018-01-25 15:03:08  uut-1     0         0           0    0
2018-01-25 15:03:08  uut-2     0         0           0    0
....
2018-01-25 15:03:17  uut-1     1         1         637    2
2018-01-25 15:03:17  uut-2     1         1        1229    2
2018-01-25 15:03:18  uut-1     0         0           0    0
2018-01-25 15:03:18  uut-2     0         0           0    0
.....

最终目标是使用 groupby('UUT') 绘制每个 UUT 的时间与任何其他剩余列(例如,'Sent'、Received'、'Latency(ms)')

【问题讨论】:

    标签: python pandas dataframe time-series


    【解决方案1】:

    它并不整洁,但您可以使用以下代码做您想做的事情。


    1.复制

    idx = ['2018-01-25 15:03:05', '2018-01-25 15:03:05', '2018-01-25 15:03:17', '2018-01-25 15:03:17','2018-01-25 15:03:29', '2018-01-25 15:03:29']
    dt = pd.DatetimeIndex(idx)
    arrays = [
      dt,
      ['uut1', 'uut2', 'uut1', 'uut2', 'uut1', 'uut2']
    ]
    tuples = list(zip(*arrays))
    index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
    
    data = pd.DataFrame({
          'a' : range(1, 7),
          'b' : range(1, 7)},
          index=index)
    

    2。操纵

    data_manipulated = data.reset_index('second')
    for second, df_gb in data_manipulated.groupby('second'):
        vars()['df_{}'.format(second)] = df_gb.resample('1s').first().fillna(0)
    
    df_uut1['second'] = 'uut1'
    df_uut2['second'] = 'uut2'
    

    df_uut1['first'] = df_uut1.index.values
    df_uut1.index = range(len(df_uut1))
    
    df_uut2['first'] = df_uut2.index.values
    df_uut2.index = range(len(df_uut2), len(df_uut2)*2)
    

    result = df_uut1.append(df_uut2)
    result.index = [result['first'], result['second']]
    result = result[['a', 'b']].astype(int)
    result.sort_index(ascending=True, inplace=True)
    

    3.结果


    这是你想要做的事情吗?同样,代码本身并不那么可读。不过我想你可以自己做的更好。

    【讨论】:

      【解决方案2】:

      我最终使用了重新采样

      data2 = data.reset_index(level=[1])
                          second  a  b
      first                           
      2018-01-25 15:03:05   uut1  1  1
      2018-01-25 15:03:05   uut2  2  2
      2018-01-25 15:03:17   uut1  3  3
      2018-01-25 15:03:17   uut2  4  4
      2018-01-25 15:03:29   uut1  5  5
      2018-01-25 15:03:29   uut2  6  6
      

      然后分组

      grouped = data2.groupby('second')
      <pandas.core.groupby.DataFrameGroupBy object at 0x0000000005AB6E48>
      
      # the groupby dataframe looks something like this:
      grouped.get_group('uut1')
                     second  a  b
      first                           
      2018-01-25 15:03:05   uut1  1  1
      2018-01-25 15:03:17   uut1  3  3
      2018-01-25 15:03:29   uut1  5  5
      

      现在对每个组重新采样并用 0 填充上采样数据:

      grouped_df = grouped.get_group(key).resample('1S').asfreq(0)
      

      最后,将第二个中的所有“0”条目替换为“uut1” grouped_df['second'] = 'uut1'

      最终的数据框如下所示:

      grouped.get_group('uut1')
                          second  a  b
      first                           
      2018-01-25 15:03:05   uut1  1  1
      2018-01-25 15:03:06   uut1  0  0
      2018-01-25 15:03:07   uut1  0  0
      2018-01-25 15:03:08   uut1  0  0
      ...
      2018-01-25 15:03:27   uut1  0  0
      2018-01-25 15:03:28   uut1  0  0
      2018-01-25 15:03:29   uut1  5  5
      

      【讨论】:

      • 太棒了!确实,您的某些代码比我的更具可读性。感谢分享!
      猜你喜欢
      • 2016-06-13
      • 2014-04-06
      • 2016-10-16
      • 2015-02-22
      • 1970-01-01
      • 1970-01-01
      • 2013-12-04
      • 2020-06-08
      相关资源
      最近更新 更多