Bird
Raised Fist0
Pandasdata~20 mins

Selecting columns by name in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Column Selector Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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)
A
   A  C
0  1  7
1  2  8
2  3  9
B
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9
C
   B
0  4
1  5
2  6
DKeyError: "['A', 'C'] not in index"
Attempts:
2 left
💡 Hint
Remember to use double brackets to select multiple columns by name.
❓ data_output
intermediate
2: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)
A<class 'pandas.core.frame.DataFrame'>\n B\n0 30\n1 40
B<class 'pandas.core.series.Series'>\n0 30\n1 40\nName: B, dtype: int64
C<class 'list'>\n[30, 40]
DKeyError: 'B'
Attempts:
2 left
💡 Hint
Selecting a single column by name returns a Series, not a DataFrame.
🔧 Debug
advanced
2: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)
ATypeError: unhashable type: 'list'
BAttributeError: 'DataFrame' object has no attribute 'X'
CIndexError: list index out of range
DKeyError: "['X', 'Y'] not in index"
Attempts:
2 left
💡 Hint
Check if the columns exist in the DataFrame before selecting.
🚀 Application
advanced
2: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
Aresult = df[cols]
Bresult = df.cols
Cresult = df[['cols']]
Dresult = df.loc[:, cols]
Attempts:
2 left
💡 Hint
Use the list variable directly inside the brackets to select columns.
🧠 Conceptual
expert
2: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?
ABoth return a Series but with different index labels
Bdf['A'] returns a DataFrame, df[['A']] returns a Series
Cdf['A'] returns a Series, df[['A']] returns a DataFrame
DBoth return a DataFrame but with different column order
Attempts:
2 left
💡 Hint
Think about the data structure type returned by each selection.

Practice

(1/5)
1.

Which syntax correctly selects a single column named 'Age' from a pandas DataFrame df?

easy
A. df.Age[]
B. df['Age']
C. df[['Age']]
D. df('Age')

Solution

  1. Step 1: Understand single column selection syntax

    Using single brackets with the column name as a string selects one column and returns a Series.
  2. Step 2: Compare options

    df['Age'] is the correct syntax. Double brackets return a DataFrame, and other options are invalid syntax.
  3. Final Answer:

    df['Age'] -> Option B
  4. Quick Check:

    Single brackets select one column [OK]
Hint: Use single brackets for one column, double for many [OK]
Common Mistakes:
  • Using double brackets for single column returns DataFrame, not Series
  • Trying to use parentheses instead of brackets
  • Using dot notation with brackets incorrectly
2.

Which of the following is the correct syntax to select multiple columns 'Name' and 'Salary' from a DataFrame df?

easy
A. df[['Name', 'Salary']]
B. df['Name', 'Salary']
C. df['Name' & 'Salary']
D. df.Name, df.Salary

Solution

  1. Step 1: Recall syntax for selecting multiple columns

    Multiple columns must be passed as a list inside double brackets.
  2. Step 2: Evaluate options

    Only df[['Name', 'Salary']] correctly uses a list inside double brackets. Others are invalid or return separate Series.
  3. Final Answer:

    df[['Name', 'Salary']] -> Option A
  4. Quick Check:

    Double brackets + list select multiple columns [OK]
Hint: Use double brackets with list for multiple columns [OK]
Common Mistakes:
  • Using single brackets with multiple column names separated by comma
  • Using bitwise AND (&) instead of list
  • Trying to select columns separately without combining
3.

Given the DataFrame df below, what is the output of df[['City', 'Age']]?

import pandas as pd
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30], 'City': ['NY', 'LA']}
df = pd.DataFrame(data)
medium
A.
   City  Name
0    NY  Alice
1    LA    Bob
B.
   Name  Age
0  Alice   25
1    Bob   30
C.
   City  Age
0    NY   25
1    LA   30
D.
   Age  City
0   25    NY
1   30    LA

Solution

  1. Step 1: Understand the selection

    The code selects columns 'City' and 'Age' in that order from the DataFrame.
  2. Step 2: Check the output order and values

    The output DataFrame has columns 'City' then 'Age' with values from original data.
  3. Final Answer:

    City Age 0 NY 25 1 LA 30 -> Option C
  4. Quick Check:

    Selected columns match output order [OK]
Hint: Output columns appear in the order listed inside double brackets [OK]
Common Mistakes:
  • Mixing column order in output
  • Selecting wrong columns
  • Confusing Series output with DataFrame output
4.

What is wrong with this code to select columns 'A' and 'B' from DataFrame df?

df['A', 'B']
medium
A. It raises a KeyError because tuple ('A', 'B') is not a column
B. It returns a DataFrame with columns 'A' and 'B'
C. It returns a Series with combined values of 'A' and 'B'
D. It raises a SyntaxError due to wrong brackets

Solution

  1. Step 1: Analyze the indexing syntax

    Using single brackets with multiple comma-separated strings creates a tuple key, not a list.
  2. Step 2: Understand pandas column selection behavior

    Since ('A', 'B') is a tuple, pandas looks for a column with that exact tuple name, which does not exist, causing KeyError.
  3. Final Answer:

    It raises a KeyError because tuple ('A', 'B') is not a column -> Option A
  4. Quick Check:

    Single brackets with comma create tuple key, causing KeyError [OK]
Hint: Use double brackets with list for multiple columns to avoid KeyError [OK]
Common Mistakes:
  • Using single brackets with multiple column names separated by comma
  • Expecting a DataFrame but getting error
  • Confusing tuple with list in indexing
5.

You have a DataFrame df with columns 'Name', 'Age', 'Salary', and 'Department'. You want to create a new DataFrame with only 'Name' and 'Salary', but also want to rename 'Salary' to 'Income'. Which code correctly does this?

hard
A.
new_df = df[['Name', 'Salary']].rename(columns=['Name', 'Income'])
B.
new_df = df['Name', 'Salary'].rename(columns={'Salary': 'Income'})
C.
new_df = df[['Name', 'Salary']].rename({'Salary': 'Income'})
D.
new_df = df[['Name', 'Salary']].rename(columns={'Salary': 'Income'})

Solution

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

    Use double brackets with a list: df[['Name', 'Salary']] to get a DataFrame with those columns.
  2. Step 2: Rename 'Salary' to 'Income'

    Use rename(columns={'Salary': 'Income'}) to rename the column correctly.
  3. Final Answer:

    new_df = df[['Name', 'Salary']].rename(columns={'Salary': 'Income'}) -> Option D
  4. Quick Check:

    Select columns then rename with columns dict [OK]
Hint: Select columns first, then rename with columns dict [OK]
Common Mistakes:
  • Using single brackets for multiple columns
  • Passing rename argument without 'columns=' keyword
  • Passing list instead of dict to rename
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