簡體   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