繁体   English   中英

带有多索引的熊猫数据帧重新采样时间序列索引

[英]Panda dataframe re-sample timeseries index with multiindex

我有一组数据,如何将其时间戳重新采样到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的时间与其他任何剩余列的关系(例如,“已发送”,“已接收”,“延迟(ms)”)

它不是很整洁,但是您可以使用以下代码来完成所需的操作。


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.结果


这是您要尝试执行的操作吗? 同样,代码本身并不那么可读。 我想您可以自己做得更好。

我最终使用了重新采样

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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