I have a dataframe and I wanted to apply a certain function on a set of columns. Something like:
data[["A","B","C","D","E"]].apply(some_func, axis=1)
In the some_func function, the first step is extracting out all the column values into separate variables.
def some_func(x):
a,b,c,d,e = x # or x.tolist()
#Some more processing
To reproduce, the result, use
x = pd.Series([1,2,3,4,5], index=["A","B","C","D","E"])
Now, my question is, why does
%%timeit
a,b,c,d,e = x.tolist()
Output:
538 ns ± 2.82 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
perform better than
%%timeit
a,b,c,d,e = x
Output:
1.61 µs ± 15.5 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)