最近开始从我的安全位置 (R) 扩展到 Python,并且对 Pandas
中的单元定位/选择感到有些困惑。我已阅读文档,但我很难理解各种本地化/选择选项的实际含义。
我是否有理由在 at
和 iat
上使用 .loc
或 .iloc
或相反亦然? 在什么情况下我应该使用哪种方法?
Note: future readers be aware that this question is old and was written before pandas v0.20 when there used to exist a function called
.ix
. This method was later split into two -loc
andiloc
- to make the explicit distinction between positional and label based indexing. Please beware thatix
was discontinued due to inconsistent behavior and being hard to grok, and no longer exists in current versions of pandas (>= 1.0).
最佳答案
loc: 仅适用于索引
iloc:在职位上工作
at: 获取标量值。这是一个非常快的定位
iat: 获取标量值。这是一个非常快的 iloc
还有,
at
andiat
are meant to access a scalar, that is, a single element in the dataframe, whileloc
andiloc
are ments to access several elements at the same time, potentially to perform vectorized operations.
http://pyciencia.blogspot.com/2015/05/obtener-y-filtrar-datos-de-un-dataframe.html
https://stackoverflow.com/questions/28757389/