I set out to determine whether lambda expressions are slower or faster than named functions when applied to a pandas DataFrame, not expecting to find a substantial difference. To my surprise, the lambda approach was substantially slower in the following example. Is this consistently true? if so, why?
import pandas as pd
import numpy as np
import cmath
df = pd.DataFrame(np.random.randint(1,100,(100000,3)), columns=['col1','col2', 'col3'])
#Named function approach:
def quad_roots(row):
'''Function that calculates roots of a quadratic equation'''
a = row['col1']; b = row['col2']; c = row['col3']
dis = (b ** 2) - (4 * a * c)
root1 = (-b - cmath.sqrt(dis)) / (2 * a)
root2 = (-b + cmath.sqrt(dis)) / (2 * a)
return np.round(root1,2), np.round(root2,2)
df['roots_named'] = df.apply(quad_roots, axis=1)
#Lambda approach
df['roots_lambda'] = df.apply(lambda x: ((np.round((-x['col2'] - cmath.sqrt((x['col2'] ** 2) - (4 * x['col1'] * x['col3']))) / (2 * x['col1']),2) ),
(np.round((-x['col2'] - cmath.sqrt((x['col2'] ** 2) - (4 * x['col1'] * x['col3']))) / (2 * x['col1']),2) ))
, axis=1)