[英]different ways to access pandas.DataFrame columns
What is the difference between df[columns]
and df.loc[:,columns]
, both as lvalue and rvalue? df[columns]
和df.loc[:,columns]
之间的区别是lvalue和rvalue有什么区别?
They seem to be interchangeable from the behavioral POV: 它们似乎可以与行为POV互换:
>>> df = pd.DataFrame({'x':[1,2,3],'y':['a','b','c']})
>>> df[['x']].equals(df.loc[:,['x']])
True
>>> df.loc[:,'z'] = df.x.apply(str) + df.y
>>> df['a'] = df.x.apply(str) + df.y
>>> df
x y z a
0 1 a 1a 1a
1 2 b 2b 2b
2 3 c 3c 3c
I know there is a document somewhere answering this in excruciating details (and I am sure I even saw it once, but a link would be nice), but I am looking at an "executive summary", so to speak. 我知道有一个文件在某个地方回答这个令人难以忍受的细节(我相信我甚至只看过一次,但链接会很好),但我正在看一个“执行摘要”,可以这么说。
Specifically : is one a shortcut for the other, or there is some semantic difference? 具体来说 :一个是另一个的快捷方式,还是有一些语义差异?
PS. PS。 This is prompted by the message
这是由消息提示的
~/.virtualenvs/wilbur/lib/python2.7/site-packages/pandas/core/indexing.py:465: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
〜/ .virtualenvs / wilbur / lib / python2.7 / site-packages / pandas / core / indexing.py:465:SettingWithCopyWarning:尝试在DataFrame的切片副本上设置一个值。 Try using .loc[row_indexer,col_indexer] = value instead
尝试使用.loc [row_indexer,col_indexer] = value
what about timing comparison for a 300K rows DF? 300K行DF的时序比较怎么样?
In [22]: big = pd.concat([df] * 10**5, ignore_index=True)
In [23]: %timeit -n 1 -r 1 big['n1'] = big.x.apply(str) + big.y
1 loop, best of 1: 266 ms per loop
In [24]: %timeit -n1 -r 1 big.ix[:, 'n2'] = big.x.apply(str) + big.y
1 loop, best of 1: 317 ms per loop
In [25]: %timeit -n 1 -r 1 big.loc[:, 'n3'] = big.x.apply(str) + big.y
1 loop, best of 1: 333 ms per loop
In [26]: %timeit -n 1 -r 1 big.insert(len(big.columns), 'n4', big.x.apply(str) + big.y)
1 loop, best of 1: 266 ms per loop
In [27]: big.shape
Out[27]: (300000, 6)
In [28]: big.head()
Out[28]:
x y n1 n2 n3 n4
0 1 a 1a 1a 1a 1a
1 2 b 2b 2b 2b 2b
2 3 c 3c 3c 3c 3c
3 1 a 1a 1a 1a 1a
4 2 b 2b 2b 2b 2b
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.