Challenge - 5 Problems
Column Selector Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of selecting multiple columns from a DataFrame
What is the output of this code snippet when selecting columns 'A' and 'C' from the DataFrame?
Data Analysis Python
import pandas as pd df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9] }) result = df[['A', 'C']] print(result)
Attempts:
2 left
💡 Hint
Remember to use double brackets to select multiple columns in pandas.
✗ Incorrect
Selecting multiple columns from a DataFrame requires passing a list of column names inside double brackets. This returns a new DataFrame with only those columns.
❓ data_output
intermediate1:30remaining
Number of columns after selection
How many columns does the resulting DataFrame have after selecting columns 'X' and 'Y' from this DataFrame?
Data Analysis Python
import pandas as pd df = pd.DataFrame({ 'X': [10, 20], 'Y': [30, 40], 'Z': [50, 60] }) selected = df[['X', 'Y']] print(len(selected.columns))
Attempts:
2 left
💡 Hint
Count the columns you selected explicitly.
✗ Incorrect
Selecting columns 'X' and 'Y' results in a DataFrame with exactly those two columns, so the count is 2.
🔧 Debug
advanced2:00remaining
Identify the error when selecting columns
What error does this code raise when trying to select columns 'A' and 'D' from the DataFrame?
Data Analysis Python
import pandas as pd df = pd.DataFrame({ 'A': [1, 2], 'B': [3, 4], 'C': [5, 6] }) result = df[['A', 'D']] print(result)
Attempts:
2 left
💡 Hint
Check if all column names exist in the DataFrame.
✗ Incorrect
Trying to select a column that does not exist ('D') raises a KeyError indicating the missing column.
🚀 Application
advanced2:30remaining
Selecting columns based on data type
Given a DataFrame with mixed data types, which code snippet correctly selects only the numeric columns?
Data Analysis Python
import pandas as pd import numpy as np df = pd.DataFrame({ 'num1': [1, 2, 3], 'num2': [4.5, 5.5, 6.5], 'text': ['a', 'b', 'c'], 'bool': [True, False, True] })
Attempts:
2 left
💡 Hint
Use pandas method to select columns by data type.
✗ Incorrect
The select_dtypes method with include=['number'] selects all numeric columns regardless of their names.
🧠 Conceptual
expert3:00remaining
Effect of chained indexing on column selection
What is the main risk of using chained indexing like df['A']['B'] when selecting columns in pandas?
Attempts:
2 left
💡 Hint
Think about how pandas handles views and copies.
✗ Incorrect
Chained indexing can return a copy, so changes may not affect the original DataFrame, causing bugs.