0
0
Pandasdata~20 mins

Selecting multiple columns in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple Columns Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
   A  B
0  1  4
1  2  5
2  3  6
BKeyError: "['A', 'C'] not in index"
C
   B  C
0  4  7
1  5  8
2  6  9
D
   A  C
0  1  7
1  2  8
2  3  9
Attempts:
2 left
💡 Hint
Selecting multiple columns requires passing a list of column names inside double brackets.
data_output
intermediate
1: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))
A2
B3
C4
D1
Attempts:
2 left
💡 Hint
Count the number of columns inside the list used for selection.
🔧 Debug
advanced
2: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)
ATypeError: unhashable type: 'list'
BSyntaxError: invalid syntax
CKeyError: "('a', 'b')"
DAttributeError: 'DataFrame' object has no attribute 'a, b'
Attempts:
2 left
💡 Hint
Check how multiple columns should be selected using brackets.
🚀 Application
advanced
2: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]
})
Aselected = df[[col for col in df.columns if 'p' in col]]
Bselected = df[df.columns.str.contains('p')]
Cselected = df.loc[:, df.columns == 'p']
Dselected = df[df.columns.filter('p')]
Attempts:
2 left
💡 Hint
Use a list comprehension to filter column names containing 'p'.
🧠 Conceptual
expert
2: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?
ASelecting a single column with df['col'] returns a DataFrame, while selecting multiple columns with df[['col1', 'col2']] returns a Series.
BSelecting a single column with df['col'] returns a Series, while selecting multiple columns with df[['col1', 'col2']] returns a DataFrame.
CBoth df['col'] and df[['col1', 'col2']] return a Series.
DBoth df['col'] and df[['col1', 'col2']] return a DataFrame.
Attempts:
2 left
💡 Hint
Think about the data structure type returned by each selection method.