Pandas: improve algorithm with find substring in column
<p>I have dataframe and I try to get only string, where some column contain some strings.</p>

<p>I use:</p>

<pre><code>df_res = pd.DataFrame()
for i in substr:
    res = df[df['event_address'].str.contains(i)]
</code></pre>

<p><code>df</code> looks like:</p>

<pre><code>member_id,event_address,event_time,event_duration
g1497o1ofm5a1963,fotki.yandex.ru/users/atanusha/albums,2015-05-01 00:00:05,8
g1497o1ofm5a1963,9829192.ru/apple-iphone.html,2015-05-01 00:00:15,2
g1497o1ofm5a1963,fotki.yandex.ru/users/atanusha/album/165150?&amp;p=3,2015-05-01 00:00:17,2
g1497o1ofm5a1963,fotki.yandex.ru/tags/%D0%B1%D0%BE%D1%81%D0%B8%D0%BA%D0%BE%D0%BC?text=%D0%B1%D0%BE%D1%81%D0%B8%D0%BA%D0%BE%D0%BC&amp;search_author=utpaladev&amp;&amp;p=2,2015-05-01 00:01:31,10
g1497o1ofm5a1963,3gmaster.net,2015-05-01 00:01:41,6
g1497o1ofm5a1963,fotki.yandex.ru/search.xml?text=%D0%B1%D0%BE%D1%81%D0%B8%D0%BA%D0%BE%D0%BC&amp;&amp;p=2,2015-05-01 00:02:01,6
g1497o1ofm5a1963,fotki.yandex.ru/search.xml?text=%D0%B1%D0%BE%D1%81%D0%B8%D0%BA%D0%BE%D0%BC&amp;search_author=Sunny-Fanny&amp;,2015-05-01 00:02:31,2
g1497o1ofm5a1963,fotki.9829192.ru/apple-iphone.html,2015-05-01 00:03:25,6
</code></pre>

<p>and <code>substr</code> is: </p>

<pre><code>123.ru/gadgets/communicators
320-8080.ru/mobilephones
3gmaster.net
3-q.ru/products/smartfony/s
9829192.ru/apple-iphone.html
9829192.ru/index.php?cat=1
acer.com/ac/ru/ru/content/group/smartphones
aj.ru
</code></pre>

<p>I get desirable result with this code, but it's loo long.
I also try to use column(<code>substr</code> it's a <code>substr = urls.url.values.tolist()</code>)
and I try</p>

<pre><code>res = df[df['event_address'].str.contains(urls.url)]
</code></pre>

<p>but it returns: </p>

<blockquote>
  <p>TypeError: 'Series' objects are mutable, thus they cannot be hashed</p>
</blockquote>

<p>Is it any way to make it more faster or maybe I'm wrong? </p>

<p>do like this:</p>

<pre><code>def check_exists(x):
    for i in substr:
        if i in x:
            return True
    return False

df2 = df.ix[df.event_address.map(check_exists)]
</code></pre>

<p>or if you like write it in one-line:</p>

<pre><code>df.ix[df.event_address.map(lambda x: any([True for i in substr if i in x]))]
</code></pre>

<hr>

python string pandas indexing substring