2016-08-17 124 views
1

我的數據框看起來像熊貓:重新採樣時間組名稱後數據幀GROUPBY

Time,       Id    A    B   C                    
2016-06-15 08:09:26.212962 115516    3   3.238  7.790000 
2016-06-15 08:10:13.863304 115517    3   0.000  8.930000 
2016-06-15 08:11:02.236033 115518    3   0.000  9.090000 
2016-06-15 08:11:52.085754 115519    3   0.000  9.420000 

如果我申請一個GROUPBY像

grouped = df.groupby(pd.TimeGrouper("5Min"), as_index=False) 

我得到的組名和團體,如:

2016-06-15 08:05:00 
    2016-06-15 08:09:26.212962 
2016-06-15 08:10:00 
    2016-06-15 08:10:13.863304 
    2016-06-15 08:11:02.236033 
    2016-06-15 08:11:52.085754 
2016-06-15 08:25:00 
    2016-06-15 08:25:41.827770 

所以我的問題是我如何重新取樣上面形成的組名並填充不存在的組與None來得到一些例如:

2016-06-15 08:05:00 
    2016-06-15 08:09:26.212962 
2016-06-15 08:10:00 
    2016-06-15 08:10:13.863304 
    2016-06-15 08:11:02.236033 
    2016-06-15 08:11:52.085754 
2016-06-15 08:15:00 
2016-06-15 08:20:00 
2016-06-15 08:25:00 
    2016-06-15 08:25:41.827770 

這可以形成一個數據幀嗎?

問候

回答

0

簡單的方法是將它們形成到另一個數據幀。使用pd.concat

frames, names = [], [] 

grouped = df.groupby(pd.TimeGrouper("5Min"), as_index=False) 
for name, group in grouped: 
    names.extend([name]) 
    frames.extend([group]) 

pd.concat(frames, keys=names) 
0

這是我現在想出來的最好的。

df.set_index('Time').groupby(pd.TimeGrouper('5T')) \ 
    .apply(lambda df: df.reset_index()).unstack() \ 
    .resample('5T').last().stack(dropna=False)