How iloc and loc Differ: Label vs. Location
Understanding the Distinction
The primary distinction between iloc and loc lies in how they access rows and columns:
-
loc: Locates data using row and column labels. These labels are typically index values or column names.
-
iloc: Locates data using row and column integer locations. These locations refer to the position of the elements in the DataFrame.
Demonstration
Consider the example DataFrame below:
Index |
Column A |
0 |
John |
1 |
Mary |
2 |
Peter |
Extracting the first 5 rows:
-
loc[:5]: Returns all rows with index labels 0 to 4 (inclusive).
-
iloc[:5]: Returns the first 5 rows at integer locations 0 to 4 (exclusive).
Clarifying the Difference
To further illustrate, consider a non-monotonic index:
Index |
Series |
49 |
a |
48 |
b |
47 |
c |
0 |
d |
1 |
e |
2 |
f |
Accessing the value at index label 0:
-
loc[0] fetches 'd' because it uses index labels.
-
iloc[0] fetches 'a' because it uses integer locations (even though the integer location of 'd' is 3).
Accessing a slice of rows:
-
loc[0:1] retrieves rows with index labels 0 and 1 (inclusive).
-
iloc[0:1] retrieves only the row at index location 0 (and does not include row 1).
Additional Considerations
-
Missing labels: loc raises a KeyError if the specified label is not in the index, while iloc returns an IndexError.
-
Boolean Series: loc can index through a Boolean Series, while iloc returns a NotImplementedError.
-
Callables: loc and iloc can both apply callables as indexers, but they handle out-of-bounds values differently.
The above is the detailed content of What's the Difference Between Pandas' `iloc` and `loc` for Data Selection?. For more information, please follow other related articles on the PHP Chinese website!