let's compare all those that work in all cases
Initial DataFrame:
arr = np.random.randint(10, 50, size=(1000, 1000))
df = pd.DataFrame(arr)
Apply:
%%timeit
df.apply(lambda x: x**3)
329 ms ± 117 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Transform:
%%timeit
df.transform(lambda x: x**3)
352 ms ± 48 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Applymap (very bad):
%%timeit
df.applymap(lambda x: x**3)
1.07 s ± 59.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Applymap is the least flexible and the slowest then why does it exist?