I have the following pandas dataframe:
import pandas as pd
import numpy as np
d = {\'age\' : [21, 45, 45, 5],
\'salary\' : [20, 40, 10, 100]}
df
Use the timeits
, Luke!
Conclusion
List comprehensions perform the best on smaller amounts of data because they incur very little overhead, even though they are not vectorized. OTOH, on larger data, loc
and numpy.where
perform better - vectorisation wins the day.
Keep in mind that the applicability of a method depends on your data, the number of conditions, and the data type of your columns. My suggestion is to test various methods on your data before settling on an option.
One sure take away from here, however, is that list comprehensions are pretty competitive—they're implemented in C and are highly optimised for performance.
Benchmarking code, for reference. Here are the functions being timed:
def numpy_where(df):
return df.assign(is_rich=np.where(df['salary'] >= 50, 'yes', 'no'))
def list_comp(df):
return df.assign(is_rich=['yes' if x >= 50 else 'no' for x in df['salary']])
def loc(df):
df = df.assign(is_rich='no')
df.loc[df['salary'] > 50, 'is_rich'] = 'yes'
return df