If you've ever been unsure whether to use loc or iloc for selecting data from a Pandas DataFrame, you're not alone. While they seem similar, the fundamental difference between label-based and integer position-based indexing can lead to unexpected results or errors. In this tutorial, we'll use a student performance dataset to demystify these two essential methods. You can refer to the source material for further reading.

Core Concept: Label (loc) vs Integer Position (iloc)
loc selects data based on row and column labels. iloc selects data based on integer positions (0-based indexing). This distinction is crucial.
Let's set up a sample DataFrame and examine the difference.
import pandas as pd
# Create sample data
data = {
'student_id': [101, 102, 103, 104, 105],
'name': ['Alice', 'Bob', 'Charlie', 'Diana', 'Edward'],
'math': [85, 58, 92, 76, 88],
'english': [90, 64, 78, 81, 95],
'science': [92, 70, 85, 79, 91]
}
df = pd.DataFrame(data)
# Set 'student_id' as the index
df.set_index('student_id', inplace=True)
print(df)
Now, let's select the same data (Bob's record) using both methods.
# Using loc: Select by label (102)
bob_loc = df.loc[102]
print("Result from loc:")
print(bob_loc)
print()
# Using iloc: Select by position (1) - zero-indexed
bob_iloc = df.iloc[1]
print("Result from iloc:")
print(bob_iloc)
Both print Bob's data, but the logic differs: loc finds the row with label 102, while iloc grabs the second row (position 1).

Common Usage Patterns in Practice
1. Selecting Multiple Rows/Columns
You can pass a list of labels or positions.
# loc: Select multiple students by label list
selected_loc = df.loc[[101, 103, 105]]
# iloc: Select multiple students by position list
selected_iloc = df.iloc[[0, 2, 4]]
2. The Critical Slicing Difference
In slicing, loc includes the end label, while iloc excludes the end position (like standard Python slicing).
# loc slicing (includes label 103)
sliced_loc = df.loc[101:103]
# iloc slicing (excludes position 3)
sliced_iloc = df.iloc[0:3]
Misunderstanding this can cause overlaps or gaps when splitting data into chunks.
3. Boolean Filtering
loc is the natural choice for filtering rows based on conditions.
# Students with math score > 80
high_math = df.loc[df['math'] > 80]
# Students with math > 70 AND science > 80
high_scores = df.loc[(df['math'] > 70) & (df['science'] > 80)]
Doing this with iloc requires converting the boolean Series to a list, which is less intuitive.

Conclusion: When to Use Which?
- Use
locwhen: Your DataFrame has meaningful labels (e.g., IDs, dates) and you want to select/filter based on them. Readability is key, or you need boolean filtering. - Use
ilocwhen: Labels are absent or meaningless, and you care about absolute positions (e.g., first 100 rows). When labels might be duplicate or changeable, and you need robust, position-based code.
Think of loc as "calling by name" and iloc as "calling by number." Choosing the right tool based on your data's structure and your task will make your Pandas workflows more efficient and error-free. Try running the code examples yourself to solidify the concepts.