Pandas: write condition to filter in dataframe
<p>I have dataframe</p>

<pre><code>member_id,event_time,event_path,event_duration
19440,"2016-08-09 08:26:48",accounts.google.com/ServiceLogin?service=mail&amp;passive=true&amp;rm=false&amp;continue=https://mail.google.com/mail/&amp;ss=1&amp;scc=1&amp;ltmpl=default&amp;ltmplcache=2&amp;emr=1&amp;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&amp;passive=true&amp;rm=false&amp;continue=https://mail.google.com/mail/&amp;ss=1&amp;scc=1&amp;ltmpl=default&amp;ltmplcache=2&amp;emr=1&amp;osid=1#identifier,0
19440,"2016-08-09 08:27:53",accounts.google.com/ServiceLogin?service=mail&amp;passive=true&amp;rm=false&amp;continue=https://mail.google.com/mail/&amp;ss=1&amp;scc=1&amp;ltmpl=default&amp;ltmplcache=2&amp;emr=1&amp;osid=1#identifier,2
19441,"2016-08-09 08:27:55",accounts.google.com/ServiceLogin?service=mail&amp;passive=true&amp;rm=false&amp;continue=https://mail.google.com/mail/&amp;ss=1&amp;scc=1&amp;ltmpl=default&amp;ltmplcache=2&amp;emr=1&amp;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
</code></pre>

<p>I need to create new column with <code>visiting</code>, and if <code>new.enjoysurvey.com/ru/survey/649/index/m_e48f6e46bf0d222e2be70bc9067730c4</code> contain in <code>event_path</code> and next <code>event_path</code> contain <code>['echo.msk.ru', 'edimdoma.ru', 'glaz.tv', 'vesti.ru']</code>, than <code>visiting == 1</code>, else - <code>2</code>.
If it complete to <code>member_id</code>, give to member_id visiting=1
Desire output</p>

<pre><code>member_id,event_time,event_path,event_duration, visiting
19440,"2016-08-09 08:26:48",accounts.google.com/ServiceLogin?service=mail&amp;passive=true&amp;rm=false&amp;continue=https://mail.google.com/mail/&amp;ss=1&amp;scc=1&amp;ltmpl=default&amp;ltmplcache=2&amp;emr=1&amp;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&amp;passive=true&amp;rm=false&amp;continue=https://mail.google.com/mail/&amp;ss=1&amp;scc=1&amp;ltmpl=default&amp;ltmplcache=2&amp;emr=1&amp;osid=1#identifier,0,2
19440,"2016-08-09 08:27:53",accounts.google.com/ServiceLogin?service=mail&amp;passive=true&amp;rm=false&amp;continue=https://mail.google.com/mail/&amp;ss=1&amp;scc=1&amp;ltmpl=default&amp;ltmplcache=2&amp;emr=1&amp;osid=1#identifier,2,2
19441,"2016-08-09 08:27:55",accounts.google.com/ServiceLogin?service=mail&amp;passive=true&amp;rm=false&amp;continue=https://mail.google.com/mail/&amp;ss=1&amp;scc=1&amp;ltmpl=default&amp;ltmplcache=2&amp;emr=1&amp;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
</code></pre>

<p>I try</p>

<pre><code>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() &gt; 1).astype(int)).replace(0, 2)
</code></pre>

<p>But it determine only size of quantity <code>event_path</code>, but I need to consider the sequence. But don't know how to do this.</p>

<p>Consider a <code>groupby.apply()</code> which uses a loop through <code>event_path</code> strings. With loop you can search adjacent elements by list indices:</p>

<pre><code>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&amp;...               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&amp;...               0         2
# 3      19440  2016-08-09 08:27:53  accounts.google.com/ServiceLogin?service=mail&amp;...               2         2
# 4      19441  2016-08-09 08:27:55  accounts.google.com/ServiceLogin?service=mail&amp;...               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
</code></pre>

<p>**NOTE: your first URL search, <code>new.enjoysurvey.com/...</code> is not contained in your posted data. Above changes this url to item in data for code demonstration.</p>

python pandas