2014-04-04 95 views
0

比方說,我有下面的用戶數據停留在一個酒店:串聯在大熊貓

   end    start  uid 
0 2014-01-02 00:00:00 2014-01-01 00:00:00 1 
1 2014-01-04 00:00:00 2014-01-02 00:00:00 1 
2 2014-02-02 00:00:00 2014-02-01 00:00:00 1 
3 2014-01-02 00:00:00 2014-01-01 00:00:00 3 

而且我想串聯,通過同一用戶的1天或更少(不同的連續住宿),從而有效地創建以下數據幀:

   end    start  uid 
0 2014-01-04 00:00:00 2014-01-01 00:00:00 1 
2 2014-02-02 00:00:00 2014-02-01 00:00:00 1 
3 2014-01-02 00:00:00 2014-01-01 00:00:00 3 

第一步驟將是groupby("uid")。但是,如何遍歷每個組的行,以便我可以使用pandas工具箱進行連接?

爲方便起見,這裏是數據幀的最小初始化:

import pandas as pd 
from datetime import datetime 
data = pd.DataFrame([{"uid":1,"start":datetime(year=2014,month=1,day=1),"end":datetime(year=2014,month=1,day=2)},{"uid":1,"start":datetime(year=2014,month=1,day=2),"end":datetime(year=2014,month=1,day=4)},{"uid":1,"start":datetime(year=2014,month=2,day=1),"end":datetime(year=2014,month=2,day=2)},{"uid":3,"start":datetime(year=2014,month=1,day=1),"end":datetime(year=2014,month=1,day=2)}]) 

回答

0

所以,這是我如何解決這一點,而無需使用任何矢量或特殊的熊貓特色。另外,這假定數據在開始,結束時進行排序。

data["discard"] = False 

grouped = data.groupby("uid") 
uids = data.uid.unique() 
maxdiff = 24 * 60 * 60 
parts = [] 
for uid in uids: 
    group = grouped.get_group(uid) 

    for x in range(1,len(group)): 
     prev = group.end.iloc[x-1] 
      curr = group.start.iloc[x] 
     difference = int((curr-prev).total_seconds()) 
     if difference<maxdiff: 
      group.start.iloc[x] = group.start.iloc[x-1] 
      group.discard.iloc[x-1] = True 

    parts.append(group) 

new_df = pd.concat(parts) 
new_df = new_df[new_df.discard==False] 
del new_df["discard"] 
print new_df