Loc vs iloc in pandas: Key Differences and When to Use Each
loc selects data by label names of rows and columns, while iloc selects data by integer position indexes. Use loc when you know the row or column labels, and iloc when you want to select by numeric position.Quick Comparison
Here is a quick side-by-side comparison of loc and iloc in pandas.
| Feature | loc | iloc |
|---|---|---|
| Selection basis | Label names of rows/columns | Integer positions of rows/columns |
| Supports slicing | Yes, label-based inclusive slicing | Yes, position-based exclusive slicing |
| Accepts boolean arrays | Yes | Yes |
| Works with row and column labels | Yes | No, only positions |
| Allows mixed label/index selection | Yes, with labels | No, only integers |
| Error on missing labels | Raises KeyError | Raises IndexError if out of bounds |
Key Differences
loc uses the actual labels of the DataFrame’s index and columns to select data. This means if your rows are labeled with dates, strings, or custom labels, loc lets you pick rows and columns by those exact names. It also includes the end label when slicing, so df.loc['a':'c'] includes rows labeled 'a', 'b', and 'c'.
On the other hand, iloc works purely with integer positions, like counting rows and columns from zero. It ignores the labels and selects rows or columns by their position number. When slicing with iloc, the end position is excluded, similar to Python’s usual slicing rules.
Because loc depends on labels, it raises a KeyError if you try to select a label that does not exist. iloc raises an IndexError if you try to select a position outside the DataFrame’s range. Both support boolean arrays for filtering, but loc is more intuitive when working with labeled data.
Code Comparison
Example using loc to select rows and columns by labels.
import pandas as pd data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Age': [25, 30, 35, 40], 'City': ['NY', 'LA', 'Chicago', 'Houston']} index_labels = ['a', 'b', 'c', 'd'] df = pd.DataFrame(data, index=index_labels) # Select rows 'b' to 'd' and columns 'Name' and 'City' result = df.loc['b':'d', ['Name', 'City']] print(result)
iloc Equivalent
Example using iloc to select rows and columns by integer positions matching the same data as above.
import pandas as pd data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Age': [25, 30, 35, 40], 'City': ['NY', 'LA', 'Chicago', 'Houston']} index_labels = ['a', 'b', 'c', 'd'] df = pd.DataFrame(data, index=index_labels) # Select rows 1 to 3 (positions) and columns 0 and 2 (positions) result = df.iloc[1:4, [0, 2]] print(result)
When to Use Which
Choose loc when you want to select data by meaningful row or column labels, especially if your DataFrame has custom or non-numeric indexes. It is clearer and less error-prone when working with labeled data.
Choose iloc when you want to select data by position, such as the first few rows or columns, or when you do not know the labels. It is useful for quick slicing by numeric index.
In summary, use loc for label-based selection and iloc for position-based selection.
Key Takeaways
loc selects data by labels; iloc selects by integer positions.loc includes the end label in slices; iloc excludes the end position.loc raises KeyError for missing labels; iloc raises IndexError for out-of-range positions.loc when working with labeled indexes and iloc for numeric position-based selection.