Python 2.7- fastest way to apply function to 2 columns of a pandas data frame
<p>I need to check if a string that is in one column of a pandas data frame is in another. 
Example data:</p>

<pre><code>aa=['mma', 'sdas', 'asdsad']*1000
t=pd.DataFrame(aa)
a=['m', 'f', 'n']*1000
t1=pd.DataFrame(a)
t2=pd.concat([t,t1], axis=1)
t2.columns=['texto', 'textito']
</code></pre>

<p>With a lambda function I get what I need, but it's too slow:</p>

<pre><code>t2['veo1'] = t2.apply(lambda row: int(row['textito'] in row['texto']),axis=1)
</code></pre>

<blockquote>
  <p>t2[:10]</p>
</blockquote>

<pre><code>    texto textito  veo1
0     mma       m     1
1    sdas       f     0
2  asdsad       n     0
3     mma       m     1
4    sdas       f     0
5  asdsad       n     0
6     mma       m     1
7    sdas       f     0
8  asdsad       n     0
9     mma       m     1
</code></pre>

<p>Is there a way to do this faster?</p>

<p>Thanks.</p>

<p>Use a comprehension and <code>zip</code></p>

<pre><code>t2['veo1'] = [int(a in b) for a, b in zip(t2.textito, t2.texto)]
</code></pre>

<p><strong><em>Better answer per @Ninja Puppy</em></strong></p>

<pre><code>t2['veo1'] = pd.Series([a in b for a, b in zip(t2.textito, t2.texto)], dtype=int)
</code></pre>

<p><strong><em>Even Better answer per @Ninja Puppy</em></strong></p>

<pre><code>from operator import contains;
t2['veo1'] = pd.Series(map(contains, t2.texto, t2.textito), dtype=int)
</code></pre>

<hr>

<p>Per @Ninja Puppy's suggestion.  Using <code>set</code> and checking for subset works in this particular situation with single character strings.  However, it would also return <code>True</code> for <code>'www'</code> in <code>'word'</code> which probably isn't what you want.</p>

<pre><code>set('www') &lt;= set('word')

True
</code></pre>

<p>Also</p>

<pre><code>set('not') &lt;= set('stone')

True
</code></pre>

<p>When</p>

<pre><code>'not' in 'stone'

False
</code></pre>

<hr>

<h3>Timing</h3>

<p><a href="http://i.stack.imgur.com/MkKXg.png" rel="nofollow"><img src="http://i.stack.imgur.com/MkKXg.png" alt="enter image description here"></a></p>

<p><strong><em>Special Note</em></strong></p>

<p>Thanks to @Ninja Puppy</p>

<p><a href="http://i.stack.imgur.com/gao0D.png" rel="nofollow"><img src="http://i.stack.imgur.com/gao0D.png" alt="enter image description here"></a></p>

<p>Notice that if we assign the <code>bool</code> values from the comprehension to a <code>pd.Series</code> and let a vectorized operation take care of the conversion to <code>int</code>, we can shave off some time.</p>

<p>We can get even more efficient if we import the <code>contains</code> operator and use python's <code>map</code></p>

python string performance function pandas