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>If space is plentiful, you could create a new DataFrame by applying <code>set</code> to the original.  Then the membership test would be much faster than using <code>in</code> with the strings.</p>

<pre><code># setup
aa=['mma', 'sdas', 'asdsad']*1000
t=pd.DataFrame(aa)
a=['m', 'f', 'n']*1000
t1=pd.DataFrame(a)
df=pd.concat([t,t1], axis=1)
df.columns=['a', 'b']

# new DataFrame  using the set of the relevant columns
df2 = df.applymap(set)
# new column based on the membership test
df['v'] = df2.b &lt;= df2.a

&gt;&gt;&gt; df[:10]
        a  b      v
0     mma  m   True
1    sdas  f  False
2  asdsad  n  False
3     mma  m   True
4    sdas  f  False
5  asdsad  n  False
6     mma  m   True
7    sdas  f  False
8  asdsad  n  False
9     mma  m   True
&gt;&gt;&gt; 
</code></pre>

python string performance function pandas