0
0
Data Analysis Pythondata~20 mins

Selecting rows (loc, iloc) in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Row Selection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
    A   B
1  20  15
3  40  35
B
    A   B
1  20  15
2  30  25
C
    A   B
2  30  25
3  40  35
D
    A   B
0  10   5
1  20  15
Attempts:
2 left
💡 Hint
Remember that iloc uses integer position and the end index is exclusive.
Predict Output
intermediate
2: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)
A
    X  Y
c  300  3
d  400  4
B
    X  Y
b  200  2
C
    X  Y
b  200  2
c  300  3
D
    X  Y
a  100  1
b  200  2
c  300  3
Attempts:
2 left
💡 Hint
loc includes the end label in slicing.
🔧 Debug
advanced
2: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)
ATypeError: cannot do positional indexing on a non-integer key
BKeyError: '1'
CIndexError: single positional indexer is out-of-bounds
DNo error, outputs the second row
Attempts:
2 left
💡 Hint
iloc expects integer positions, not strings.
data_output
advanced
2: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)
A
    B   C
x  6  10
y  7  11
z  8  12
B
    A   B
x  2   6
y  3   7
z  4   8
C
    B   C
w  5   9
x  6  10
y  7  11
D
    A   C
x  2  10
y  3  11
z  4  12
Attempts:
2 left
💡 Hint
loc selects rows by label, iloc selects columns by position.
🧠 Conceptual
expert
3:00remaining
Difference between loc and iloc in row selection
Which statement correctly describes the difference between loc and iloc when selecting rows in pandas?
A<code>loc</code> selects rows by integer position and includes the end index; <code>iloc</code> selects rows by label and excludes the end label.
B<code>loc</code> selects rows by integer position and excludes the end index; <code>iloc</code> selects rows by label and includes the end label.
C<code>loc</code> and <code>iloc</code> both select rows by label but differ in column selection methods.
D<code>loc</code> selects rows by label and includes the end label in slices; <code>iloc</code> selects rows by integer position and excludes the end index.
Attempts:
2 left
💡 Hint
Think about how slicing works differently for labels vs positions.