Bird
Raised Fist0
Pandasdata~20 mins

Selecting multiple columns in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the correct way to select multiple columns 'A' and 'B' from a pandas DataFrame df?
easy
A. df.loc['A', 'B']
B. df['A', 'B']
C. df[['A', 'B']]
D. df['A' & 'B']

Solution

  1. Step 1: Understand pandas column selection syntax

    To select multiple columns, pandas requires a list of column names inside double brackets.
  2. Step 2: Apply correct syntax for multiple columns

    Using df[['A', 'B']] correctly selects columns 'A' and 'B'. Other options are invalid syntax or select rows instead.
  3. Final Answer:

    df[['A', 'B']] -> Option C
  4. Quick Check:

    Multiple columns = list inside double brackets [OK]
Hint: Use double brackets with a list for multiple columns [OK]
Common Mistakes:
  • Using single brackets with comma-separated names
  • Using bitwise operators like & between column names
  • Using .loc incorrectly for column selection
2. Which of the following is the correct syntax to select columns 'X' and 'Y' from DataFrame df?
easy
A. df.loc[:, 'X', 'Y']
B. df.loc['X', 'Y']
C. df['X', 'Y']
D. df[['X', 'Y']]

Solution

  1. Step 1: Recall syntax for selecting multiple columns

    Using double brackets with a list of column names is the standard way: df[['X', 'Y']].
  2. Step 2: Check other options for correctness

    df.loc[:, 'X', 'Y'] causes TypeError (invalid arguments to loc); df['X', 'Y'] raises KeyError (tuple not a column); df.loc['X', 'Y'] is incorrect syntax.
  3. Final Answer:

    df[['X', 'Y']] -> Option D
  4. Quick Check:

    Double brackets with list = correct syntax [OK]
Hint: Remember: double brackets with list select multiple columns [OK]
Common Mistakes:
  • Using single brackets with multiple column names
  • Confusing .loc row and column selection syntax
  • Using tuple instead of list inside brackets
3. Given the DataFrame df below:
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9

What is the output of df[['B', 'C']]?
medium
A.
   B  C
0  4  7
1  5  8
2  6  9
B.
   A  B
0  1  4
1  2  5
2  3  6
C.
   A  C
0  1  7
1  2  8
2  3  9
D. KeyError

Solution

  1. Step 1: Identify columns requested

    The code selects columns 'B' and 'C' from the DataFrame.
  2. Step 2: Extract columns 'B' and 'C' values

    Columns 'B' and 'C' have values [[4,7],[5,8],[6,9]] for rows 0,1,2 respectively.
  3. Final Answer:

    B C 0 4 7 1 5 8 2 6 9 -> Option A
  4. Quick Check:

    Selected columns 'B' and 'C' output matches
       B  C
    0  4  7
    1  5  8
    2  6  9
    [OK]
Hint: Selected columns show only those columns with all rows [OK]
Common Mistakes:
  • Confusing columns selected and expecting all columns
  • Getting KeyError by misspelling column names
  • Selecting rows instead of columns
4. What error will occur with this code?
df = pd.DataFrame({'X':[1,2], 'Y':[3,4]})
selected = df['X', 'Y']
medium
A. TypeError
B. KeyError
C. SyntaxError
D. No error, returns both columns

Solution

  1. Step 1: Analyze the indexing syntax

    The code uses single brackets with a comma, creating a tuple key ('X', 'Y').
  2. Step 2: Understand pandas error type

    This raises KeyError because pandas looks for a column named ('X', 'Y'), which doesn't exist.
  3. Final Answer:

    KeyError -> Option B
  4. Quick Check:

    Tuple key in single brackets causes KeyError [OK]
Hint: Use list inside double brackets, not comma in single brackets [OK]
Common Mistakes:
  • Using comma inside single brackets for multiple columns
  • Expecting no error with wrong syntax
  • Confusing TypeError with KeyError
5. You have a DataFrame df with columns ['Name', 'Age', 'City', 'Salary']. You want to create a new DataFrame df_selected with only 'Name' and 'Salary'. Which code correctly does this and also resets the index?
hard
A. df_selected = df[['Name', 'Salary']].reset_index(drop=True)
B. df_selected = df.loc[:, ['Name', 'Salary']].reset_index()
C. df_selected = df[['Name', 'Salary']].reset_index()
D. df_selected = df[['Name', 'Salary']].reset_index(drop=False)

Solution

  1. Step 1: Select columns 'Name' and 'Salary'

    Use double brackets with list: df[['Name', 'Salary']] to select these columns.
  2. Step 2: Reset index without adding old index as column

    Use reset_index(drop=True) to reset index and drop old index column.
  3. Step 3: Check other options

    df.loc[:, ['Name', 'Salary']].reset_index() resets index but keeps old index as column. Using reset_index() without drop=True (default behavior) keeps the old index as a new column.
  4. Final Answer:

    df_selected = df[['Name', 'Salary']].reset_index(drop=True) -> Option A
  5. Quick Check:

    Select columns + reset index drop old index = df_selected = df[['Name', 'Salary']].reset_index(drop=True) [OK]
Hint: Select columns with double brackets, reset index with drop=True [OK]
Common Mistakes:
  • Forgetting to use double brackets for multiple columns
  • Not using drop=True in reset_index to avoid old index column
  • Using .loc without understanding index reset effect
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard