Challenge - 5 Problems
iloc Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember iloc uses integer positions for rows and columns, starting at 0, and the end index is exclusive.
✗ Incorrect
iloc[1:3, 0:2] selects rows at positions 1 and 2, and columns at positions 0 and 1. So rows with index 1 and 2, columns 'A' and 'B'.
❓ data_output
intermediate1: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)
Attempts:
2 left
💡 Hint
Count how many rows and columns are included between the start and end indices.
✗ Incorrect
Rows 2, 3, 4 are included (3 rows). Columns 1 and 2 are included (2 columns). So shape is (3, 2).
🔧 Debug
advanced1: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)
Attempts:
2 left
💡 Hint
Check the number of rows and the index used in iloc.
✗ Incorrect
The DataFrame has rows at positions 0,1,2. Accessing position 3 is out of bounds, causing IndexError.
🚀 Application
advanced2: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?Attempts:
2 left
💡 Hint
Remember that iloc slicing with a single column index returns a Series, but slicing with a range returns a DataFrame.
✗ Incorrect
df.iloc[-2:, 0:1] selects last two rows and first column as a DataFrame. Other options either return Series or are valid but return Series.
🧠 Conceptual
expert2: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)
Attempts:
2 left
💡 Hint
iloc with lists selects specific rows and columns by position, not slices.
✗ Incorrect
Rows at positions 0, 2, 4 and columns at positions 1 and 3 correspond to columns 'X' and 'Z'. Values are from the DataFrame accordingly.