Challenge - 5 Problems
Row Selection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of iloc row selection
What is the output of this code snippet selecting rows using
iloc?Data Analysis Python
import pandas as pd df = pd.DataFrame({ 'A': [10, 20, 30, 40], 'B': [5, 15, 25, 35] }) result = df.iloc[1:3] print(result)
Attempts:
2 left
💡 Hint
Remember that iloc uses integer position and the end index is exclusive.
✗ Incorrect
The iloc[1:3] selects rows at positions 1 and 2, excluding 3. So rows with index 1 and 2 are returned.
❓ Predict Output
intermediate2:00remaining
Output of loc row selection with label slicing
What will be printed by this code using
loc to select rows by label?Data Analysis Python
import pandas as pd df = pd.DataFrame({ 'X': [100, 200, 300, 400], 'Y': [1, 2, 3, 4] }, index=['a', 'b', 'c', 'd']) result = df.loc['b':'c'] print(result)
Attempts:
2 left
💡 Hint
loc includes the end label in slicing.
✗ Incorrect
Using loc with label slicing 'b':'c' includes both 'b' and 'c' rows.
🔧 Debug
advanced2:00remaining
Identify the error in iloc row selection
What error will this code raise when selecting rows with iloc?
Data Analysis Python
import pandas as pd df = pd.DataFrame({'val': [1, 2, 3]}) result = df.iloc['1'] print(result)
Attempts:
2 left
💡 Hint
iloc expects integer positions, not strings.
✗ Incorrect
Passing a string '1' to iloc causes a TypeError because iloc only accepts integers or integer lists.
❓ data_output
advanced2:00remaining
Result of mixed loc and iloc selection
Given this DataFrame, what is the output of selecting rows using loc and columns using iloc?
Data Analysis Python
import pandas as pd df = pd.DataFrame({ 'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8], 'C': [9, 10, 11, 12] }, index=['w', 'x', 'y', 'z']) result = df.loc['x':'z'].iloc[:, 1:3] print(result)
Attempts:
2 left
💡 Hint
loc selects rows by label, iloc selects columns by position.
✗ Incorrect
loc['x':'z'] selects rows with labels x, y, z. iloc[:, 1:3] selects columns at positions 1 and 2, which are 'B' and 'C'.
🧠 Conceptual
expert3:00remaining
Difference between loc and iloc in row selection
Which statement correctly describes the difference between
loc and iloc when selecting rows in pandas?Attempts:
2 left
💡 Hint
Think about how slicing works differently for labels vs positions.
✗ Incorrect
loc uses labels and includes the end label in slices, while iloc uses integer positions and excludes the end index.