Selecting Rows with iloc
Learn to select rows using position-based indexing with iloc
Selecting Rows with iloc
What is iloc?
iloc selects by position (numeric index).
Think of it as selecting by row number, starting from 0.
import pandas as pd
df = pd.DataFrame({
'Name': ['John', 'Sarah', 'Mike', 'Emma'],
'Age': [25, 30, 28, 32],
'City': ['NYC', 'LA', 'Chicago', 'Miami']
})
print(df)Select Single Row
row = df.iloc[0]
print(row)Gets first row (position 0).
Returns Series.
Select Multiple Rows
rows = df.iloc[[0, 2]]
print(rows)Gets rows at positions 0 and 2.
Returns DataFrame.
Slicing Rows
subset = df.iloc[0:2]
print(subset)Important: Excludes endpoint! Gets rows 0 and 1 only.
Same as Python list slicing.
Negative Indexing
last_row = df.iloc[-1]
print(last_row)
last_two = df.iloc[-2:]
print(last_two)-1 is last row, -2 is second-to-last.
Select Rows and Columns
value = df.iloc[0, 0]
print(value)Gets row 0, column 0.
Multiple rows and columns:
subset = df.iloc[[0, 1], [0, 2]]
print(subset)Gets:
- Rows 0, 1
- Columns 0, 2
Slicing Rows and Columns
subset = df.iloc[0:2, 0:2]
print(subset)Gets:
- Rows 0, 1
- Columns 0, 1
All Rows, Specific Columns
first_two_cols = df.iloc[:, 0:2]
print(first_two_cols): means all rows.
Specific Rows, All Columns
first_two_rows = df.iloc[0:2, :]
print(first_two_rows)Can omit second ::
first_two_rows = df.iloc[0:2]Step in Slicing
every_other = df.iloc[::2]
print(every_other)Gets rows 0, 2, 4...
Custom step:
subset = df.iloc[0:4:2]Gets rows 0 and 2.
Practice Example
The scenario: Process sales data by position.
import pandas as pd
sales = pd.DataFrame({
'Date': ['2024-01-01', '2024-01-02', '2024-01-03',
'2024-01-04', '2024-01-05', '2024-01-06'],
'Product': ['Laptop', 'Phone', 'Tablet', 'Monitor', 'Keyboard', 'Mouse'],
'Quantity': [5, 10, 7, 3, 15, 20],
'Price': [999, 599, 399, 299, 75, 25]
})
print("All data:")
print(sales)
print()
print("First sale:")
print(sales.iloc[0])
print()
print("Last sale:")
print(sales.iloc[-1])
print()
print("First 3 sales:")
print(sales.iloc[0:3])
print()
print("Last 2 sales:")
print(sales.iloc[-2:])
print()
print("Every other sale:")
print(sales.iloc[::2])
print()
print("First 3 rows, first 2 columns:")
print(sales.iloc[0:3, 0:2])
print()
print("Specific rows and columns:")
specific = sales.iloc[[0, 2, 4], [1, 3]]
print(specific)
print()
print("Calculate revenue for first 3:")
first_three = sales.iloc[0:3]
first_three_copy = first_three.copy()
first_three_copy['Revenue'] = first_three_copy['Quantity'] * first_three_copy['Price']
print(first_three_copy)Random Rows
import numpy as np
random_indices = np.random.choice(len(df), 2, replace=False)
random_rows = df.iloc[random_indices]
print(random_rows)First and Last N Rows
first_3 = df.iloc[:3]
last_3 = df.iloc[-3:]
print("First 3:", first_3)
print("Last 3:", last_3)Middle Rows
middle = df.iloc[1:-1]
print(middle)Skips first and last.
Setting Values
df.iloc[0, 0] = 'Johnny'
print(df)Multiple values:
df.iloc[0:2, 1] = 99
print(df)loc vs iloc
loc (label-based)
df.loc[0] # Row with index label 0
df.loc[0:2] # Rows 0, 1, AND 2 (includes end)
df.loc[:, 'Name'] # Column named 'Name'
df.loc[df['Age'] > 30] # Boolean indexingiloc (position-based)
df.iloc[0] # First row (position 0)
df.iloc[0:2] # Rows 0, 1 (excludes 2)
df.iloc[:, 0] # First column
# No boolean indexing with ilocWhen to Use iloc
Use iloc when:
- Selecting by position
- Don't care about index labels
- Want first/last N rows
- Slicing with numeric positions
Use loc when:
- Selecting by label
- Boolean conditions
- Custom index names
- Column names
Custom Index Example
df_custom = pd.DataFrame({
'Value': [10, 20, 30]
}, index=['A', 'B', 'C'])
print(df_custom.loc['B']) # Row with label 'B'
print(df_custom.iloc[1]) # Second row (same result)Both get same row, different methods.
Combining iloc with Operations
top_3_avg = df.iloc[0:3]['Age'].mean()
print("Average age of first 3:", top_3_avg)
last_5_sum = df.iloc[-5:]['Salary'].sum()
print("Total salary of last 5:", last_5_sum)Key Points to Remember
iloc selects by position (0, 1, 2...), not by label.
iloc[0:2] excludes endpoint - gets rows 0 and 1 only (like Python lists).
Negative indexing works: iloc[-1] is last row, iloc[-2] is second-to-last.
iloc[row, col] format where both are numeric positions.
Cannot use boolean conditions directly with iloc. Use loc for that.
Common Mistakes
Mistake 1: Confusing loc and iloc
df.iloc['Name'] # Error! iloc uses positions
df.loc[:, 'Name'] # Correct for column name
df.iloc[:, 0] # Correct for first columnMistake 2: Expecting inclusive slicing
df.iloc[0:3] # Gets rows 0, 1, 2 (excludes 3)
df.loc[0:3] # Gets rows 0, 1, 2, 3 (includes 3)Mistake 3: Boolean indexing with iloc
df.iloc[df['Age'] > 30] # Error!
df.loc[df['Age'] > 30] # CorrectMistake 4: Out of range
df.iloc[100] # Error if only 10 rows
# Check length first:
if len(df) > 100:
row = df.iloc[100]Mistake 5: Modifying slice without copy
subset = df.iloc[0:3]
subset['New'] = 1 # May affect original!
subset = df.iloc[0:3].copy() # SafeWhat's Next?
You now know how to use iloc for position-based selection. Next, you'll learn about filtering data - using conditions to select specific rows based on values.