[英]Panda dataframe re-sample timeseries index with multiindex
I have a set of data, how can I resample its time stamp to 1 second interval, and fill in the data column (other than 'UUT') with 0. 我有一组数据,如何将其时间戳重新采样到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
Output something like this: 输出如下内容:
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
.....
The ultimate goal is to use groupby('UUT') to plot each UUT's time vs any other remaining columns (eg 'Sent', Received', 'Latency(ms)') 最终目标是使用groupby('UUT')来绘制每个UUT的时间与其他任何剩余列的关系(例如,“已发送”,“已接收”,“延迟(ms)”)
It's not neat but you could be able to do things you wanted with following code. 它不是很整洁,但是您可以使用以下代码来完成所需的操作。
1. Reproduction 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. Manipulation 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. Result 3.结果
Is this something you were trying to do? 这是您要尝试执行的操作吗? Again, code itself ins't that readable.
同样,代码本身并不那么可读。 I guess you can make it better on your own though.
我想您可以自己做得更好。
I ended up using re sampling 我最终使用了重新采样
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
and then groupby 然后分组
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
Now resample each group and fill up the upsample data with 0: 现在对每个组重新采样,并用0填充上采样数据:
grouped_df = grouped.get_group(key).resample('1S').asfreq(0)
finally, replace all '0' entries in second with 'uut1' grouped_df['second'] = 'uut1' 最后,将所有第二个“ 0”条目替换为“ uut1” grouped_df ['second'] ='uut1'
The final dataframe looks like this: 最终的数据帧如下所示:
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.