loc[] and iloc[] in Pandas
Learn how to select rows, columns, and individual cells using labels and integer positions in a Pandas DataFrame.
1. Introduction to loc[] and iloc[]
Pandas provides two important indexers for selecting data:
loc[] and iloc[].
Both can select rows and columns, but they identify them
differently.
loc[] — Label-Based Selection
Use loc[] when you want to select data using
row index labels and column names.
df.loc[1] df.loc[1, "Marks"]
iloc[] — Position-Based Selection
Use iloc[] when you want to select data using
integer positions. Positions start at zero.
df.iloc[1] df.iloc[1, 3]
2. Create a Sample DataFrame
We will use the following student dataset to understand each operation.
import pandas as pd
df = pd.DataFrame({
"Name": ["Samadhan", "Pradnya", "Samiksha", "Amit"],
"Age": [21, 22, 20, 23],
"City": ["Pune", "Mumbai", "Pune", "Nashik"],
"Marks": [85, 92, 78, 88]
})
print(df)
Output
Name Age City Marks 0 Samadhan 21 Pune 85 1 Pradnya 22 Mumbai 92 2 Samiksha 20 Pune 78 3 Amit 23 Nashik 88
The default row labels are 0, 1, 2, and 3. The column labels are Name, Age, City, and Marks.
3. Select a Row Using loc[]
To select a row by its index label, provide the label inside
loc[].
print(df.loc[1])
Output
Name Pradnya Age 22 City Mumbai Marks 92 Name: 1, dtype: object
The row with index label 1 contains Pradnya's information. The result is a Series because one row was selected.
Select a row with a custom label
indexed_df = df.set_index("Name")
print(indexed_df.loc["Samadhan"])
Output
Age 21 City Pune Marks 85 Name: Samadhan, dtype: object
loc[] looks for the
label itself. The label does not have to be an integer.
4. Select a Row Using iloc[]
The iloc[] indexer selects rows by their integer
position. Since positions start at zero, position 0 is the
first row and position 1 is the second row.
print(df.iloc[0])
Output
Name Samadhan Age 21 City Pune Marks 85 Name: 0, dtype: object
Select the third row
print(df.iloc[2])
Output
Name Samiksha Age 20 City Pune Marks 78 Name: 2, dtype: object
iloc[2] means
position 2, which is the third row. It does not search for
an index label equal to 2.
5. Visual Difference Between loc[] and iloc[]
With the default index, row labels and row positions may look identical. A custom index makes the difference easier to see.
Figure 1: loc[] searches for a row label; iloc[] selects a row by its position.
In this example, loc[205] selects the row labelled
205, while iloc[1] selects the second row.
They happen to select the same row here, but they use different
selection rules.
6. Select a Specific Column Using loc[]
Use a row label and a column name to select a particular value.
print(df.loc[1, "Marks"])
Output
92
The code returns the Marks value for the row labelled 1.
Select the name and marks for one row
print(df.loc[1, ["Name", "Marks"]])
Output
Name Pradnya Marks 92 Name: 1, dtype: object
7. Select a Specific Cell Using iloc[]
With iloc[], specify the row position first and
the column position second. Both positions start at zero.
print(df.iloc[1, 3])
Output
92
Row position 1 is the second row, and column position 3 is the fourth column, Marks.
Select multiple columns by position
print(df.iloc[1, [0, 3]])
Output
Name Pradnya Marks 92 Name: 1, dtype: object
8. Select Multiple Rows with loc[]
You can pass a list of row labels to select multiple rows.
print(df.loc[[0, 2]])
Output
Name Age City Marks 0 Samadhan 21 Pune 85 2 Samiksha 20 Pune 78
Select multiple rows and specific columns
print(df.loc[[0, 2], ["Name", "Marks"]])
Output
Name Marks 0 Samadhan 85 2 Samiksha 78
9. Select Multiple Rows with iloc[]
Use a list of integer positions to select particular rows by position.
print(df.iloc[[0, 2]])
Output
Name Age City Marks 0 Samadhan 21 Pune 85 2 Samiksha 20 Pune 78
Select rows and columns by position
print(df.iloc[[0, 2], [0, 3]])
Output
Name Marks 0 Samadhan 85 2 Samiksha 78
10. Slicing with loc[] and iloc[]
Both indexers support slicing, but the end boundary behaves differently.
Slicing with loc[]
print(df.loc[0:2, ["Name", "Marks"]])
For label-based slicing, both the start and end labels are included when the labels are present and the index is appropriately ordered.
Output
Name Marks 0 Samadhan 85 1 Pradnya 92 2 Samiksha 78
Slicing with iloc[]
print(df.iloc[0:2, [0, 3]])
For position-based slicing, the starting position is included and the stopping position is excluded.
Output
Name Marks 0 Samadhan 85 1 Pradnya 92
loc[0:2] includes label 2, while
iloc[0:2] stops before position 2.
11. Visual Guide: Understanding Slicing
Figure 2: loc label slicing includes the ending label; iloc position slicing excludes the ending position.
12. Select Rows Using Boolean Conditions
You can also use a Boolean condition with either indexer. This is useful when you want to select rows that satisfy a condition.
With loc[]
print(df.loc[df["Marks"] >= 85, ["Name", "Marks"]])
Output
Name Marks 0 Samadhan 85 1 Pradnya 92 3 Amit 88
With iloc[]
mask = df["Marks"] >= 85 print(df.iloc[mask.to_numpy(), [0, 3]])
Here, the Boolean mask is converted to a NumPy array so that
iloc[] receives a positional Boolean selector.
In everyday filtering, loc[] is generally more
convenient when the condition is built from DataFrame columns.
13. Comparison Table
| Feature | loc[] | iloc[] |
|---|---|---|
| Selection type | Label-based | Integer-position-based |
| Row selector | Index label | Zero-based position |
| Column selector | Column name or label | Integer column position |
| Single row | df.loc[1] |
df.iloc[1] |
| Single cell | df.loc[1, "Marks"] |
df.iloc[1, 3] |
| Slice end | Included for label slices | Excluded for positional slices |
| Typical use | Meaningful index labels and column names | Exact row and column positions |
14. Common Errors to Avoid
Error 1: Using a missing label
Selecting a label that does not exist can raise a
KeyError.
Error 2: Using an invalid position
An integer position outside the valid range can raise
an IndexError.
Error 3: Forgetting zero-based positions
iloc[0] is the first row, not the second.
Error 4: Confusing row labels with positions
After setting a custom index, a row label may not match its integer position. Choose the appropriate indexer based on what you intend to select.
15. Practice Exercises
Exercise 1: Row Selection
- Select the first row using
iloc[]. - Select the row labelled 2 using
loc[]. - Select the third row using
iloc[].
Exercise 2: Cell Selection
- Retrieve Pradnya's marks using
loc[]. - Retrieve the same value using
iloc[]. - Select the Name and City columns for the first two rows.
Exercise 3: Slicing
- Select labels 0 through 2 using
loc[]. - Select the first two rows using
iloc[]. - Explain why the two slices return different numbers of rows.
Suggested solutions
# First row by position print(df.iloc[0]) # Row with label 2 print(df.loc[2]) # Third row by position print(df.iloc[2]) # Pradnya's marks print(df.loc[1, "Marks"]) print(df.iloc[1, 3]) # First two rows and selected columns print(df.loc[0:1, ["Name", "City"]]) # Label-based slice print(df.loc[0:2]) # Position-based slice print(df.iloc[0:2])
16. Revision Questions
- What is label-based selection?
- What is position-based selection?
- What is the difference between
loc[]andiloc[]? - How do you select a specific cell using each method?
- What is the difference between
loc[0:3]andiloc[0:3]? - Why is
iloc[0]the first row? - How can you select multiple rows and columns together?
- What errors can occur when selecting missing labels or invalid positions?
Summary
The loc[] indexer selects rows and columns
using labels, while iloc[] selects them using
integer positions. Both methods can retrieve individual
rows, specific cells, multiple rows, and selected columns.
Remember that label-based slices generally include their ending label, whereas position-based slices exclude the ending position. Choosing the correct indexer makes your Pandas code more accurate and easier to understand.