Challenge - 5 Problems
Column Selector Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Selecting multiple columns by name
What is the output of this code snippet that selects columns 'A' and 'C' from the DataFrame?
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
Remember to use double brackets to select multiple columns by name.
✗ Incorrect
Using df[['A', 'C']] selects only columns 'A' and 'C' from the DataFrame, returning a new DataFrame with those columns.
❓ data_output
intermediate2:00remaining
Selecting a single column returns a Series
What is the type and output of selecting column 'B' from the DataFrame below?
Pandas
import pandas as pd df = pd.DataFrame({ 'A': [10, 20], 'B': [30, 40], 'C': [50, 60] }) result = df['B'] print(type(result)) print(result)
Attempts:
2 left
💡 Hint
Selecting a single column by name returns a Series, not a DataFrame.
✗ Incorrect
df['B'] returns a pandas Series object representing the column 'B'. The type confirms this.
🔧 Debug
advanced2:00remaining
Identify the error when selecting columns
What error does this code raise when trying to select columns 'X' and 'Y' from the DataFrame?
Pandas
import pandas as pd df = pd.DataFrame({ 'A': [1, 2], 'B': [3, 4] }) result = df[['X', 'Y']] print(result)
Attempts:
2 left
💡 Hint
Check if the columns exist in the DataFrame before selecting.
✗ Incorrect
Selecting columns that do not exist in the DataFrame raises a KeyError listing the missing columns.
🚀 Application
advanced2:00remaining
Select columns by name using a list variable
Given a list of column names, which option correctly selects those columns from the DataFrame?
Pandas
import pandas as pd cols = ['A', 'C'] df = pd.DataFrame({ 'A': [5, 6], 'B': [7, 8], 'C': [9, 10] }) result = None # Fill in the correct selection
Attempts:
2 left
💡 Hint
Use the list variable directly inside the brackets to select columns.
✗ Incorrect
Using df[cols] selects the columns named in the list cols. df.loc[:, cols] also works but is not in options as correct here.
🧠 Conceptual
expert2:00remaining
Difference between selecting columns by single vs double brackets
What is the main difference between df['A'] and df[['A']] when selecting columns in pandas?
Attempts:
2 left
💡 Hint
Think about the data structure type returned by each selection.
✗ Incorrect
Selecting a single column with single brackets returns a Series. Using double brackets returns a DataFrame with one column.