2017-07-10 37 views
1

我对访问大熊猫列的各种方式之间的性能差异感到困惑。访问大熊猫列的最快方式

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 

我知道最后一个变种比较慢,因为它可以设置值,而不仅仅是访问。但为什么df.adf['a']慢?无论中间结果被缓存,这似乎都是真的。

回答

1

Here是一个链接,它解释了.访问和[]访问之间的区别。

还应考虑这些运营商的行为文档

getitem(用于[])和getattr(为.)方法浅析。

.似乎通过函数调用来访问列,从而以更少的时间比它作为字典键值

+0

感谢您访问的[]。这很有帮助。然而,在熊猫的情况下,函数调用'.'需要两次[']',这与您的建议相反。有任何想法吗? – amball