Pandas - identifying dataframe values that start with value in a list
<p>Say I have the following dataframe:</p>

<pre><code>&gt;&gt;&gt; import pandas as pd
&gt;&gt;&gt; d=pd.DataFrame()
&gt;&gt;&gt; d['A']=['12345','12354','76','4']
&gt;&gt;&gt; d['B']=['4442','2345','33','5']
&gt;&gt;&gt; d['C']=['5553','4343','33','5']
&gt;&gt;&gt; d
       A     B     C
0  12345  4442  5553
1  12354  2345  4343
2     76    33    33
3      4     5     5
</code></pre>

<p>And say I have 3 values of interest:</p>

<pre><code>&gt;&gt;&gt; vals=['123','76']
</code></pre>

<p>I am interested in determining which values in my dataframe start with any of the values in my list. There are 3 cases in my example: (0,A) starts with 123; (1,A) starts with 123; and (2,A) starts with 76. </p>

<p>Is there a way I can do this without looping through each of my values?</p>

<p>If I were interested in matching values exactly I could just do:</p>

<pre><code>&gt;&gt;&gt; d.isin(vals)
       A      B      C
0  False  False  False
1  False  False  False
2   True  False  False
3  False  False  False
&gt;&gt;&gt; 
</code></pre>

<p>And if I was interested in whether the values start with 1 particular value I could do:</p>

<pre><code>&gt;&gt;&gt; d.applymap(lambda x:x.startswith('123'))
       A      B      C
0   True  False  False
1   True  False  False
2  False  False  False
3  False  False  False
&gt;&gt;&gt; 
</code></pre>

<p>But how can I combine these two to find any value that starts with any value in my list?</p>

<p>You can construct a regex pattern and test each column in turn using <code>apply</code> with a lambda calling <code>str.contains</code>:</p>

<pre><code>In [9]:
vals=['123','76']
v = ['^' + x for x in vals]
d.apply(lambda x: x.str.contains('|'.join(v)))

Out[9]:
       A      B      C
0   True  False  False
1   True  False  False
2   True  False  False
3  False  False  False
</code></pre>

<p>The resulting regex pattern:</p>

<pre><code>In [10]:
'|'.join(v)

Out[10]:
'^123|^76'
</code></pre>

python pandas