Challenge - 5 Problems
Index Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of setting a column as index?
Given the DataFrame:
What will be printed?
import pandas as pd
df = pd.DataFrame({"Name": ["Alice", "Bob"], "Age": [25, 30]})
df2 = df.set_index("Name")
print(df2)What will be printed?
Pandas
import pandas as pd df = pd.DataFrame({"Name": ["Alice", "Bob"], "Age": [25, 30]}) df2 = df.set_index("Name") print(df2)
Attempts:
2 left
💡 Hint
Setting a column as index moves that column to the row labels.
✗ Incorrect
Using set_index("Name") makes the 'Name' column the index (row labels). The DataFrame shows 'Age' as the only column with 'Name' as the index.
📝 Syntax
intermediate2:00remaining
Which code correctly sets 'ID' as index in pandas?
Choose the code snippet that correctly sets the 'ID' column as the index of DataFrame df.
Pandas
import pandas as pd df = pd.DataFrame({"ID": [1, 2], "Value": [100, 200]})
Attempts:
2 left
💡 Hint
The column name must be a string inside quotes.
✗ Incorrect
Option A correctly passes the column name as a string. Option A and D use ID without quotes causing NameError. Option A passes a list which works but returns a DataFrame with a single-level index, not a MultiIndex.
❓ optimization
advanced2:00remaining
How to set a column as index modifying the original DataFrame?
You want to set the 'Date' column as index in DataFrame df and keep the change without creating a new DataFrame. Which option does this efficiently?
Pandas
import pandas as pd df = pd.DataFrame({"Date": ["2023-01-01", "2023-01-02"], "Sales": [200, 250]})
Attempts:
2 left
💡 Hint
Look for the parameter that changes the DataFrame in place.
✗ Incorrect
Option C uses inplace=True to modify df directly. Option C creates a new DataFrame but does not modify df in place. Option C sets index but does not remove the 'Date' column. Option C assigns a DataFrame to index which is invalid.
🔧 Debug
advanced2:00remaining
Why does this code raise an error?
Consider this code:
Why does it raise an error?
import pandas as pd
df = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
df.set_index(A)Why does it raise an error?
Pandas
import pandas as pd df = pd.DataFrame({"A": [1, 2], "B": [3, 4]}) df.set_index(A)
Attempts:
2 left
💡 Hint
Check if the column name is passed as a string or variable.
✗ Incorrect
The code uses A without quotes, so Python looks for a variable named A which is not defined, causing NameError.
🧠 Conceptual
expert2:00remaining
What happens if you set a column as index but it contains duplicate values?
If you run df.set_index('Category') where 'Category' column has duplicate values, what is the effect on the DataFrame index?
Attempts:
2 left
💡 Hint
Think about whether pandas requires index labels to be unique.
✗ Incorrect
Pandas allows index labels to be duplicated. Setting a column with duplicates as index does not raise errors and keeps all rows.