Fastest way to access pandas column

Viewed 1184

I am confused by the difference in performance between the various ways to access a pandas column.

In [1]: df = pd.DataFrame([[1,1,1],[2,2,2]],columns=['a','b','c'])

In [2]: %timeit df['a']
The slowest run took 75.37 times longer than the fastest. This could
mean that an intermediate result is being cached.
100000 loops, best of 3: 3.12 µs per loop

In [3]: %timeit df.a
The slowest run took 5.14 times longer than the fastest. This could
mean that an intermediate result is being cached.
100000 loops, best of 3: 6.59 µs per loop

In [4]: %timeit df.loc[:,'a']
10000 loops, best of 3: 55 µs per loop

I understand that the last variant is slower because it enables the values to be set, not just accessed. But why is df.a slower than df['a']? This seems true regardless of the intermediate results being cached.

1 Answers
Related