2016-09-21 75 views
0

篩選我有數據幀熊貓:寫狀況數據幀

member_id,event_time,event_path,event_duration 
19440,"2016-08-09 08:26:48",accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#identifier,0 
19440,"2016-08-09 08:27:04",ebesucher.ru/surfbar/Ochotona,25 
19440,"2016-08-09 08:27:53",accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#identifier,0 
19440,"2016-08-09 08:27:53",accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#identifier,2 
19441,"2016-08-09 08:27:55",accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#password,1 
19441,"2016-08-09 08:27:58",neobux.com/m/l/,0 
19441,"2016-08-09 08:27:59",neobux.com/m/l/,0 
19441,"2016-08-09 08:28:01",http://new.enjoysurvey.com/ru/survey/649/index/m_e48f6e46bf0d222e2be70bc9067730c423423,11 
19441,"2016-08-09 08:28:12",echo.msk.ru ,1 
19441,"2016-08-09 08:28:15",neobux.com/m/l/?vl=A206591715C607425417A51CDE023499,2 

我需要與visiting創建新列,如果new.enjoysurvey.com/ru/survey/649/index/m_e48f6e46bf0d222e2be70bc9067730c4包含event_path和未來event_path包含['echo.msk.ru', 'edimdoma.ru', 'glaz.tv', 'vesti.ru'],比visiting == 1,否則 - 2。 如果完成對member_id,給member_id來訪= 1個 慾望輸出

member_id,event_time,event_path,event_duration, visiting 
19440,"2016-08-09 08:26:48",accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#identifier,0,2 
19440,"2016-08-09 08:27:04",n,25,2 
19440,"2016-08-09 08:27:53",accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#identifier,0,2 
19440,"2016-08-09 08:27:53",accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#identifier,2,2 
19441,"2016-08-09 08:27:55",accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#password,1,1 
19441,"2016-08-09 08:27:58",neobux.com/m/l/,0,1 
19441,"2016-08-09 08:27:59",neobux.com/m/l/,0,1 
19441,"2016-08-09 08:28:01",http://new.enjoysurvey.com/ru/survey/649/index/m_e48f6e46bf0d222e2be70bc9067730c423423,11,1 
19441,"2016-08-09 08:28:12",echo.msk.ru ,1,1 
19441,"2016-08-09 08:28:15",neobux.com/m/l/?vl=A206591715C607425417A51CDE023499,2,1 

我嘗試

df['visiting'] = df.groupby("member_id").event_path.transform(lambda g: (g.isin(["new.enjoysurvey.com/ru/survey/649/index/m_e48f6e46bf0d222e2be70bc9067730c4", 'echo.msk.ru', 'edimdoma.ru', 'glaz.tv', 'vesti.ru']).sum() > 1).astype(int)).replace(0, 2) 

但它確定數量event_path只有大小,但我需要考慮的順序。但不知道如何做到這一點。

+0

Next'event_path'?你的意思是下一排?此外,這是沒有意義的:*如果它完成到member_id,給予member_id訪問= 1 *。 – Parfait

+0

@Parfait我的意思是如果在下一個字符串中有'event_path' –

回答

1

考慮使用通過event_path字符串循環的groupby.apply()。隨着循環中,您可以通過列表索引搜索相鄰的元素:

def findevent(row): 
    event_paths = row['event_path'].tolist()  
    row['visiting'] = 2 

    for i in range(len(event_paths)): 
     if 'new.enjoysurvey.com/ru/survey/649/index/m_e48f6e46bf0d222e2be70bc9067730c423423' in event_paths[i] and \ 
          event_paths[i+1] in ['echo.msk.ru', 'edimdoma.ru', 'glaz.tv', 'vesti.ru']: 
      row['visiting'] = 1 
      break    
    return(row) 

df = df.groupby(['member_id']).apply(findevent) 
print(df) 

# member_id   event_time           event_path event_duration visiting 
# 0  19440 2016-08-09 08:26:48 accounts.google.com/ServiceLogin?service=mail&...    0   2 
# 1  19440 2016-08-09 08:27:04      ebesucher.ru/surfbar/Ochotona    25   2 
# 2  19440 2016-08-09 08:27:53 accounts.google.com/ServiceLogin?service=mail&...    0   2 
# 3  19440 2016-08-09 08:27:53 accounts.google.com/ServiceLogin?service=mail&...    2   2 
# 4  19441 2016-08-09 08:27:55 accounts.google.com/ServiceLogin?service=mail&...    1   1 
# 5  19441 2016-08-09 08:27:58         neobux.com/m/l/    0   1 
# 6  19441 2016-08-09 08:27:59         neobux.com/m/l/    0   1 
# 7  19441 2016-08-09 08:28:01 http://new.enjoysurvey.com/ru/survey/649/index...    11   1 
# 8  19441 2016-08-09 08:28:12          echo.msk.ru    1   1 
# 9  19441 2016-08-09 08:28:15 neobux.com/m/l/?vl=A206591715C607425417A51CDE0...    2   1 

**注意:你的第一個網址搜索,new.enjoysurvey.com/...不包含在您發佈的數據。上面的代碼演示將此網址更改爲數據中的項目。