by oneoff786 · 3y ago · 0 comments
Pretty much everything you need in pandas is as performant as you ought to need for doing tabular data manipulation in Python. Except dataframe.apply
df = pd.DataFrame({"foo": np.random.randn(100000)})
pandas map: df["foo"].map(lambda x: x * 2)
18.1 ms ± 109 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)pandas apply:
df["foo"].apply(lambda x: x * 2)
17.9 ms ± 46.6 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)Vectorised function, using underlying numpy operations:
df["foo"] * 2
267 µs ± 11.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)Series.map is not compiling your lambda's to C and running it. If there is a built-in method available it usually will be faster. Notable exception are pandas str methods which devolve into Python code but generally with more overhead than map/apply.