Pandas: How to make apply on dataframe faster? : <p>Consider this pandas example where I'm calculating column <code>C</code> by multiplying <code>A</code> with <code>B</code> and a <code>float</code> if a certain condition is fulfilled using <code>apply</code> with a <code>lambda</code> function:</p>

<pre><code>import pandas as pd
df = pd.DataFrame({'A':[1,2,3,4,5,6,7,8,9],'B':[9,8,7,6,5,4,3,2,1]})

df['C'] = df.apply(lambda x: x.A if x.B &gt; 5 else 0.1*x.A*x.B, axis=1)
</code></pre>

<p>The expected result would be:</p>

<pre><code>   A  B    C
0  1  9  1.0
1  2  8  2.0
2  3  7  3.0
3  4  6  4.0
4  5  5  2.5
5  6  4  2.4
6  7  3  2.1
7  8  2  1.6
8  9  1  0.9
</code></pre>

<p>The problem is that this code is slow and I need to do this operation on a dataframe with around 56 million rows.</p>

<p>The <code>%timeit</code>-result of the above lambda operation is:</p>

<pre><code>1000 loops, best of 3: 1.63 ms per loop
</code></pre>

<p>Going from the calculation time and also the memory usage when doing this on my large dataframe I presume this operation uses intermediary series while doing the calculations.</p>

<p>I tried to formulate it in different ways including using temporary columns, but every alternative solution I came up with is even slower.</p>

<p>Is there a way to get the result I need in a different and faster way, e.g. by using <code>numpy</code>?</p>
