Pandas is one of the most powerful libraries in Python for data analysis and manipulation. To work with data efficiently, you need to understand indexing and selection techniques. Two commonly used methods in Pandas are iloc
and loc
, which may seem similar but have key differences.
🔍 What is iloc
in Pandas?
iloc
stands for integer location. It is used for index-based selection. This means it selects data by row and column numbers (positions), not by labels.
👉 Syntax
DataFrame.iloc[row_index, column_index]
✅ Example
import pandas as pd
# Sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'Paris', 'London']}
df = pd.DataFrame(data)
# Select the first row and second column (Age)print(df.iloc[0, 1]) # Output: 25
Here, 0
means the first row, and 1
means the second column (Age).
🏷️ What is loc
in Pandas?
loc
stands for location by label. It is used for label-based selection. This means you select rows and columns by their names or labels instead of their positions.
👉 Syntax
DataFrame.loc[row_label, column_label]
✅ Example
# Select Alice's Age using labelsprint(df.loc[0, 'Age']) # Output: 25
Here, 0
is the row label (index), and 'Age'
is the column label.
⚖️ Key Differences Between iloc
and loc
Feature | iloc (Integer Location) 🔢 | loc (Label Location) 🏷️ |
---|
Selection type | Position-based | Label-based |
Input values | Integers (0, 1, 2…) | Labels (index names, column names) |
Example | df.iloc[0, 1] → 25 | df.loc[0, 'Age'] → 25 |
Supports slices | Yes (by position) | Yes (by labels) |
Errors | Raises error if index is out of range | Raises error if label does not exist |
📝 Practical Examples
1️⃣ Selecting Multiple Rows and Columns with iloc
# Select first 2 rows and first 2 columnsprint(df.iloc[0:2, 0:2])
2️⃣ Selecting Multiple Rows and Columns with loc
# Select rows with index 0 and 1, and columns Name and Ageprint(df.loc[0:1, ['Name', 'Age']])
⚠️ Common Mistakes to Avoid
❌ Using labels with iloc
(will throw an error).
❌ Using integer positions with loc
(works only if index labels are integers, but not recommended).
❌ Forgetting that iloc
slicing is exclusive of the end index, while loc
slicing is inclusive of the end label.
✅ Example
print(df.iloc[0:2]) # Rows 0 and 1print(df.loc[0:2]) # Rows 0, 1, and 2
🎯 When to Use iloc
vs loc
Use iloc
when working with row/column positions (e.g., during iteration, numerical indexing).
Use loc
when working with row/column labels (e.g., selecting by column names or custom index).
🚀 Conclusion
Understanding the difference between iloc
and loc
is crucial for efficient data selection and manipulation in Pandas.
With these tools, you can navigate DataFrames confidently and avoid common pitfalls in Python data analysis.