Indexing with loc & iloc0%
Indexing with loc & iloc
Beginner12 min read•Updated: Jul 11, 2026
Indexing with loc & iloc
Reading Time: 8 Mins | Level: Beginner to Advanced
1. The Core Indexers in Pandas
Accessing subsets of data in Pandas is achieved primarily through two explicit indexing accessors:
- 1
loc: Label-based indexing (select by column names and row index labels). - 2
iloc: Integer position-based indexing (select by integer offset: 0, 1, 2, ...).
Golden Rule:
loc uses names/labels, iloc uses integer positions.2. Setting Up Sample Data
Python
import pandas as pd
data = {
'Name': ['Aarav', 'Priya', 'Rohan', 'Sneha', 'Kabir'],
'City': ['Delhi', 'Mumbai', 'Bengaluru', 'Noida', 'Agra'],
'Score': [92, 88, 95, 79, 85]
}
# Using custom index labels
df = pd.DataFrame(data, index=['R101', 'R102', 'R103', 'R104', 'R105'])
print(df)
3. Label-Based Indexing with .loc[row_label, col_label]
A. Selecting Single Cell or Single Row
Python
# Get row 'R101' as a Series
row_101 = df.loc['R101']
# Get specific cell: Row 'R102', Column 'Score'
score_102 = df.loc['R102', 'Score'] # Returns 88
B. Slicing with .loc (INCLUSIVE of endpoints!)
Crucial Distinction: In standard Python slicing
[0:3], the stop endpoint 3 is excluded. But with loc, both the start label and the stop label are INCLUDED!Python
# Rows from R102 through R104 inclusive, and columns 'Name' through 'Score'
subset = df.loc['R102':'R104', 'Name':'Score']
print(subset)
4. Integer-Based Indexing with .iloc[row_idx, col_idx]
.iloc operates strictly on 0-based integer positions, adhering to standard Python slice rules (stop position is EXCLUDED):
Python
# Get the first row (index 0)
first_row = df.iloc[0]
# Get the last row using negative indexing
last_row = df.iloc[-1]
# Slice first 3 rows (0, 1, 2) and first 2 columns (0, 1)
sub_matrix = df.iloc[0:3, 0:2]
print(sub_matrix)
5. Summary Cheat Table
| Feature | df.loc[] | df.iloc[] |
|---|---|---|
| Lookup Mode | By Label / Name | By Integer Position |
| Row Selection | df.loc['R101'] | df.iloc[0] |
| Slice Endpoints | Inclusive ('A':'C' includes C) | Exclusive (0:3 stops at 2) |
| Boolean Masking | Supported (df.loc[df['Score'] > 90]) | Not directly supported |
Interactive Quiz
Multiple Choice Questions
Question 1
What does the 'i' in
.iloc stand for?Question 2
How does slicing endpoint behavior differ between
.loc and .iloc?Question 3
Which command selects the very last row of any DataFrame regardless of its index labels?
Question 4
Given a DataFrame with index
['A', 'B', 'C'] and column 'Revenue', how do you access the Revenue of row 'B' using label-based indexing?Question 5
What error occurs if you pass a string column name like
'Score' into .iloc?Next Lesson
Boolean Indexing & DataFrame Querying
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Reading JSON, SQL Databases & Parquet Files | Boolean Indexing & DataFrame Querying |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.