Challenge - 5 Problems
Multiple Columns 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?
Pandas
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
Selecting multiple columns requires passing a list of column names inside double brackets.
✗ Incorrect
The code selects columns 'A' and 'C' from the DataFrame. The output shows only these two columns with their values.
❓ data_output
intermediate1:30remaining
Number of columns selected
How many columns does the resulting DataFrame have after running this code?
Pandas
import pandas as pd df = pd.DataFrame({ 'X': [10, 20], 'Y': [30, 40], 'Z': [50, 60], 'W': [70, 80] }) selected = df[['Y', 'W']] print(len(selected.columns))
Attempts:
2 left
💡 Hint
Count the number of columns inside the list used for selection.
✗ Incorrect
The code selects columns 'Y' and 'W', so the resulting DataFrame has exactly 2 columns.
🔧 Debug
advanced2:00remaining
Identify the error when selecting multiple columns
What error will this code raise?
Pandas
import pandas as pd df = pd.DataFrame({ 'a': [1, 2], 'b': [3, 4] }) result = df['a', 'b'] print(result)
Attempts:
2 left
💡 Hint
Check how multiple columns should be selected using brackets.
✗ Incorrect
Using df['a', 'b'] tries to access a single key which is a tuple ('a', 'b'), causing a KeyError because such a column does not exist.
🚀 Application
advanced2:30remaining
Selecting columns dynamically based on a condition
Given a DataFrame with columns 'temp', 'humidity', 'pressure', and 'wind', which code snippet selects only columns whose names contain the letter 'p'?
Pandas
import pandas as pd df = pd.DataFrame({ 'temp': [22, 23], 'humidity': [30, 45], 'pressure': [1012, 1013], 'wind': [5, 7] })
Attempts:
2 left
💡 Hint
Use a list comprehension to filter column names containing 'p'.
✗ Incorrect
Option A correctly uses a list comprehension to select columns with 'p' in their names. Other options either use invalid syntax or methods incorrectly.
🧠 Conceptual
expert2:00remaining
Understanding the difference between single and multiple column selection
Which statement correctly explains the difference between selecting a single column and multiple columns in pandas DataFrame?
Attempts:
2 left
💡 Hint
Think about the data structure type returned by each selection method.
✗ Incorrect
Selecting a single column returns a pandas Series (one-dimensional), while selecting multiple columns returns a DataFrame (two-dimensional).