2016-09-21 13 views
0

에 필터링 조건을 쓰기 나는 dataframe에게 내가 visiting에 새 열을 만들 필요가팬더 : dataframe

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 

있고, new.enjoysurvey.com/ru/survey/649/index/m_e48f6e46bf0d222e2be70bc9067730c4event_path에 포함되어 있으며 다음 event_path 다른, visiting == 1보다 ['echo.msk.ru', 'edimdoma.ru', 'glaz.tv', 'vesti.ru']가 포함 된 경우 - 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

다음'event_path'? 다음 행을 의미합니까? 또한, 이것은 의미가 없습니다 : * member_id를 완료하면 member_id visit = 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 

** 참고 : 첫 번째 URL 검색, new.enjoysurvey.com/...은 당신의 게시 된 데이터에 포함되지 않습니다. 위의 코드 데모에 대한 데이터 항목을이 URL을 변경하십시오.