Bird
Raised Fist0
Pandasdata~20 mins

iloc for position-based selection in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
iloc Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this iloc selection?
Given the DataFrame df below, what is the output of df.iloc[1:3, 0:2]?
Pandas
import pandas as pd

data = {'A': [10, 20, 30, 40], 'B': [5, 15, 25, 35], 'C': [100, 200, 300, 400]}
df = pd.DataFrame(data)

result = df.iloc[1:3, 0:2]
print(result)
A
    A   C
1  20  200
2  30  300
B
    A   B
0  10   5
1  20  15
C
    A   B
1  20  15
2  30  25
D
    B   C
1  15  200
2  25  300
Attempts:
2 left
💡 Hint
Remember iloc uses integer positions for rows and columns, starting at 0, and the end index is exclusive.
❓ data_output
intermediate
1:30remaining
How many rows and columns does this iloc slice return?
Given a DataFrame df with 5 rows and 4 columns, what is the shape of df.iloc[2:5, 1:3]?
Pandas
import pandas as pd
import numpy as np

df = pd.DataFrame(np.arange(20).reshape(5,4), columns=list('ABCD'))

result = df.iloc[2:5, 1:3].shape
print(result)
A(3, 2)
B(2, 3)
C(3, 3)
D(2, 2)
Attempts:
2 left
💡 Hint
Count how many rows and columns are included between the start and end indices.
🔧 Debug
advanced
1:30remaining
What error does this iloc code raise?
Consider the code below. What error will it raise when executed?
Pandas
import pandas as pd

data = {'X': [1, 2, 3], 'Y': [4, 5, 6]}
df = pd.DataFrame(data)

result = df.iloc[3, 1]
print(result)
AKeyError: 3
BIndexError: single positional indexer is out-of-bounds
CValueError: cannot index with vector containing NA / NaN values
DTypeError: unhashable type: 'slice'
Attempts:
2 left
💡 Hint
Check the number of rows and the index used in iloc.
🚀 Application
advanced
2:00remaining
Select the correct iloc code to get the last two rows and first column
You have a DataFrame df with 10 rows and 4 columns. Which iloc code correctly selects the last two rows and only the first column?
Adf.iloc[-2:, 0:1]
Bdf.iloc[8:10, 0]
Cdf.iloc[8:10, 0:1]
Ddf.iloc[-2:, 0]
Attempts:
2 left
💡 Hint
Remember that iloc slicing with a single column index returns a Series, but slicing with a range returns a DataFrame.
🧠 Conceptual
expert
2:30remaining
What is the output of this complex iloc selection?
Given the DataFrame df below, what is the output of df.iloc[[0, 2, 4], [1, 3]]?
Pandas
import pandas as pd
import numpy as np

np.random.seed(0)
df = pd.DataFrame(np.arange(20).reshape(5,4), columns=['W', 'X', 'Y', 'Z'])

result = df.iloc[[0, 2, 4], [1, 3]]
print(result)
A
    X   Z
0   1   3
1   5   7
2   9  11
B
    W   Y
0   0   2
2   8  10
4  16  18
C
    X   Z
1   5   7
3  13  15
4  17  19
D
    X   Z
0   1   3
2   9  11
4  17  19
Attempts:
2 left
💡 Hint
iloc with lists selects specific rows and columns by position, not slices.

Practice

(1/5)
1. What does df.iloc[2] select from a pandas DataFrame df?
easy
A. The third row of the DataFrame by position
B. The row with index label 2
C. The third column of the DataFrame by position
D. The column with index label 2

Solution

  1. Step 1: Understand iloc usage

    iloc selects rows or columns by integer position, starting at 0.
  2. Step 2: Interpret df.iloc[2]

    This selects the row at position 2, which is the third row counting from zero.
  3. Final Answer:

    The third row of the DataFrame by position -> Option A
  4. Quick Check:

    iloc uses position, so 2 means third row [OK]
Hint: Remember: iloc counts rows/columns from zero position [OK]
Common Mistakes:
  • Confusing iloc with loc (label-based selection)
  • Thinking iloc uses index labels instead of positions
  • Mixing row and column selection
2. Which of the following is the correct syntax to select the first 3 rows and first 2 columns using iloc?
easy
A. df.iloc[3, 2]
B. df.iloc[1:3, 1:2]
C. df.iloc[0:2, 0:3]
D. df.iloc[:3, :2]

Solution

  1. Step 1: Understand slicing with iloc

    Slices in iloc are start:stop and select positions from start up to but not including stop.
  2. Step 2: Apply slice for first 3 rows and 2 columns

    First 3 rows are positions 0,1,2 so slice is :3. First 2 columns are positions 0,1 so slice is :2.
  3. Final Answer:

    df.iloc[:3, :2] -> Option D
  4. Quick Check:

    Slices exclude stop, so :3 means 0,1,2 [OK]
Hint: Use slice :n to get first n positions [OK]
Common Mistakes:
  • Using wrong slice ranges that exclude needed rows/columns
  • Confusing row and column order in iloc
  • Using single integers instead of slices for multiple rows/columns
3. Given the DataFrame:
import pandas as pd
df = pd.DataFrame({
  'A': [10, 20, 30],
  'B': [40, 50, 60],
  'C': [70, 80, 90]
})
print(df.iloc[1, 2])

What is the output?
medium
A. 50
B. 30
C. 80
D. 60

Solution

  1. Step 1: Identify row and column positions

    iloc[1, 2] means row at position 1 (second row), column at position 2 (third column).
  2. Step 2: Find value at row 1, column 2

    Row 1 values: A=20, B=50, C=80. Column 2 is 'C', so value is 80.
  3. Final Answer:

    80 -> Option C
  4. Quick Check:

    Row 1, Col 2 = 80 [OK]
Hint: Remember iloc[row, column] uses zero-based positions [OK]
Common Mistakes:
  • Mixing up row and column positions
  • Using index labels instead of positions
  • Selecting wrong column number
4. What is wrong with this code snippet?
df.iloc[1:3, 2:1]

It aims to select rows 1 and 2 and columns 2 and 1.
medium
A. iloc cannot slice columns, only rows
B. Column slice 2:1 is empty because stop is less than start
C. Row slice 1:3 is invalid syntax
D. The code will select columns 1 and 2 correctly

Solution

  1. Step 1: Analyze column slice 2:1

    Slices select positions from start up to but not including stop. Here start=2, stop=1, which is reversed and results in an empty selection.
  2. Step 2: Check row slice 1:3

    Row slice 1:3 selects rows at positions 1 and 2 correctly.
  3. Final Answer:

    Column slice 2:1 is empty because stop is less than start -> Option B
  4. Quick Check:

    Slices with stop less than start return empty [OK]
Hint: Slice stop must be greater than start to select data [OK]
Common Mistakes:
  • Assuming reversed slices select columns in reverse
  • Thinking iloc syntax is invalid for slices
  • Confusing iloc with loc label slicing
5. You have a DataFrame df with 5 rows and 4 columns. You want to select the last 2 rows and the last 3 columns using iloc. Which code correctly does this?
hard
A. df.iloc[2:, 1:]
B. df.iloc[2:4, 1:3]
C. df.iloc[-2:, -2:]
D. df.iloc[3:5, 1:4]

Solution

  1. Step 1: Understand row positions for last 2 rows

    With 5 rows (positions 0 to 4), last 2 rows are positions 3 and 4, so slice is 3:5 or equivalently -2:.
  2. Step 2: Understand column positions for last 3 columns

    With 4 columns (positions 0 to 3), last 3 columns are positions 1, 2, 3, so slice is 1:4 or equivalently -3:.
  3. Step 3: Match correct iloc syntax

    Using negative indices, df.iloc[-2:, -3:] would be correct. Among options, df.iloc[2:, 1:] selects rows from position 2 to end (rows 2,3,4) and columns from position 1 to end (columns 1,2,3), which includes the last 2 rows and last 3 columns.
  4. Final Answer:

    df.iloc[2:, 1:] -> Option A
  5. Quick Check:

    Rows 2-4 and columns 1-3 selected correctly [OK]
Hint: Count positions from zero; slice stop is one past last index [OK]
Common Mistakes:
  • Using negative slices incorrectly
  • Off-by-one errors in slice ranges
  • Confusing row and column slice order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard