0
0
Data Analysis Pythondata~10 mins

Selecting rows (loc, iloc) in Data Analysis Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Selecting rows (loc, iloc)
Start with DataFrame
Choose selection method
loc
Select rows by
label/index
Return selected rows
End
You start with a DataFrame, choose either loc or iloc to select rows by label or position, then get the selected rows.
Execution Sample
Data Analysis Python
import pandas as pd

df = pd.DataFrame({
    'Name': ['Anna', 'Bob', 'Cara', 'Dan'],
    'Age': [23, 35, 45, 28]
})

row_loc = df.loc[1]
row_iloc = df.iloc[1]
This code creates a DataFrame and selects the second row using loc and iloc.
Execution Table
StepActionSelection MethodIndex UsedResulting Row
1Create DataFrame--{0: Name=Anna, Age=23; 1: Name=Bob, Age=35; 2: Name=Cara, Age=45; 3: Name=Dan, Age=28}
2Select row with locloc1 (label){Name: Bob, Age: 35}
3Select row with ilociloc1 (position){Name: Bob, Age: 35}
4Select row with locloc3 (label){Name: Dan, Age: 28}
5Select row with ilociloc3 (position){Name: Dan, Age: 28}
6Try loc with label not in indexloc5 (label)KeyError raised
7Try iloc with position out of rangeiloc5 (position)IndexError raised
💡 Execution stops after errors or when all selections are done.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
dfEmpty{0: Anna, 23; 1: Bob, 35; 2: Cara, 45; 3: Dan, 28}{0: Anna, 23; 1: Bob, 35; 2: Cara, 45; 3: Dan, 28}{0: Anna, 23; 1: Bob, 35; 2: Cara, 45; 3: Dan, 28}{0: Anna, 23; 1: Bob, 35; 2: Cara, 45; 3: Dan, 28}
row_locUndefined{Name: Bob, Age: 35}{Name: Bob, Age: 35}{Name: Dan, Age: 28}{Name: Dan, Age: 28}
row_ilocUndefinedUndefined{Name: Bob, Age: 35}{Name: Bob, Age: 35}{Name: Dan, Age: 28}
Key Moments - 3 Insights
Why does df.loc[5] cause an error but df.iloc[5] also causes an error?
Both cause errors because loc looks for a label '5' which does not exist in the index, and iloc looks for position 5 which is outside the DataFrame range. See execution_table rows 6 and 7.
Does df.loc[1] select the same row as df.iloc[1]?
Yes, in this example both select the second row because the index labels are default integers matching positions. See execution_table rows 2 and 3.
What is the difference between loc and iloc in selecting rows?
loc selects rows by index label, iloc selects rows by integer position. This means loc depends on the DataFrame's index labels, iloc always uses zero-based positions. See concept_flow and execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What row does df.iloc[1] select?
ARow with Name 'Cara' and Age 45
BRow with Name 'Bob' and Age 35
CRow with Name 'Anna' and Age 23
DRow with Name 'Dan' and Age 28
💡 Hint
Check the 'Resulting Row' column at step 3 in execution_table.
At which step does df.loc[5] cause an error?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look for 'KeyError raised' in the execution_table.
If the DataFrame index was ['a', 'b', 'c', 'd'], what would df.loc[1] do?
ARaise a KeyError
BSelect the second row
CSelect the first row
DSelect the row with label '1'
💡 Hint
Recall loc selects by label, so if '1' is not an index label, it raises an error.
Concept Snapshot
Selecting rows in pandas:
- Use df.loc[label] to select by index label.
- Use df.iloc[position] to select by integer position.
- loc includes the label; iloc uses zero-based position.
- loc raises KeyError if label not found.
- iloc raises IndexError if position out of range.
Full Transcript
This lesson shows how to select rows from a pandas DataFrame using loc and iloc. The DataFrame has four rows with default integer index labels 0 to 3. Using df.loc[1] selects the row with label 1, which is the second row. Using df.iloc[1] selects the row at position 1, also the second row. Both return the row with Name 'Bob' and Age 35. Trying to select df.loc[5] causes a KeyError because label 5 does not exist. Trying df.iloc[5] causes an IndexError because position 5 is outside the DataFrame. The key difference is loc uses labels, iloc uses positions. This is important when the index is not default integers. Understanding these helps avoid errors and select data correctly.