在使用 Pandas 进行数据操作时,iloc 和 loc 是两种常用的切片方法,经常会引起混淆。了解它们的根本区别对于高效的数据管理至关重要。
标签与位置
loc 和 iloc 之间的主要区别在于它们如何选择数据:
示例:
考虑具有包含字母的非单调索引的 DataFrame df:
import pandas as pd df = pd.DataFrame({'col1': ['a', 'b', 'c', 'd', 'e', 'f']}, index=[49, 48, 47, 0, 1, 2])
loc(基于标签切片):
iloc(基于位置的切片):
主要区别:
下表突出显示了 loc 和 iloc 在各种场景下的区别:
Object | Description | loc | iloc |
---|---|---|---|
0 | Single item | Value at index label 0 ('d') | Value at index location 0 ('a') |
0:1 | Slice | Two rows (labels 0 and 1) | One row (first row at location 0) |
1:47 | Slice with out-of-bounds end | Zero rows (empty Series) | Five rows (location 1 onwards) |
1:47:-1 | Slice with negative step | Three rows (labels 1 back to 47) | Zero rows (empty Series) |
[2, 0] | Integer list | Two rows with given labels | Two rows with given locations |
s > 'e' | Boolean series (indicating true values) | One row (containing 'f') | NotImplementedError |
(s>'e').values | Boolean array | One row (containing 'f') | Same as loc |
999 | Integer object not in index | KeyError | IndexError (out of bounds) |
-1 | Integer object not in index | KeyError | Returns last value in s |
lambda x: x.index[3] | Callable applied to series (here returning 3rd item in index) | s.loc[s.index[3]] | s.iloc[s.index[3]] |
以上是Pandas `loc` 和 `iloc` 在数据选择方面有什么区别?的详细内容。更多信息请关注PHP中文网其他相关文章!