How do you select a single column named 'Age' from a pandas DataFrame?
Use df['Age'] to select the column named 'Age' from the DataFrame df. This returns a Series with the data from that column.
Click to reveal answer
beginner
How can you select multiple columns, for example 'Name' and 'Age', from a pandas DataFrame?
Use df[['Name', 'Age']] to select multiple columns. This returns a new DataFrame with only those columns.
Click to reveal answer
beginner
What happens if you try to select a column name that does not exist in the DataFrame?
pandas will raise a KeyError because the column name is not found in the DataFrame.
Click to reveal answer
intermediate
How do you select columns by name using the .loc accessor?
Use df.loc[:, ['Name', 'Age']] to select columns by name. The colon : means select all rows, and the list specifies the columns.
Click to reveal answer
intermediate
Can you select columns by name using dot notation like df.Age? When is this recommended?
Yes, you can use df.Age to select a column named 'Age'. However, this works only if the column name is a valid Python identifier and does not conflict with DataFrame methods. It's safer to use df['Age'].
Click to reveal answer
Which syntax selects the column 'Salary' from DataFrame df?
Adf['Salary', 'Age']
Bdf.Salary()
Cdf.loc['Salary']
Ddf['Salary']
✗ Incorrect
Use df['Salary'] to select a single column by name.
How do you select columns 'Name' and 'Department' together?
Adf[['Name', 'Department']]
Bdf.Name.Department
Cdf.loc['Name', 'Department']
Ddf['Name', 'Department']
✗ Incorrect
Use a list inside double brackets: df[['Name', 'Department']].
What does df.loc[:, ['Age', 'Salary']] do?
ASelects all rows and columns 'Age' and 'Salary'
BSelects rows 'Age' and 'Salary'
CSelects columns 'Age' and 'Salary' only for first row
DRaises an error
✗ Incorrect
The colon means all rows, and the list selects the columns.
What error occurs if you select a non-existing column like df['Height']?
AValueError
BTypeError
CKeyError
DIndexError
✗ Incorrect
pandas raises a KeyError if the column is missing.
Which is the safest way to select a column named 'class'?
Adf.class
Bdf['class']
Cdf.loc['class']
Ddf.class()
✗ Incorrect
Use df['class'] because 'class' is a Python keyword and dot notation may fail.
Explain how to select one or more columns by name from a pandas DataFrame.
Think about single vs multiple columns and the loc accessor.
You got /3 concepts.
What are the risks or errors when selecting columns by name, and how can you avoid them?
Consider what happens if the column name is missing or unusual.
You got /3 concepts.
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
Step 1: Understand single column selection syntax
Using single brackets with the column name as a string selects one column and returns a Series.
Step 2: Compare options
df['Age'] is the correct syntax. Double brackets return a DataFrame, and other options are invalid syntax.
Final Answer:
df['Age'] -> Option B
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
Step 1: Recall syntax for selecting multiple columns
Multiple columns must be passed as a list inside double brackets.
Step 2: Evaluate options
Only df[['Name', 'Salary']] correctly uses a list inside double brackets. Others are invalid or return separate Series.
Final Answer:
df[['Name', 'Salary']] -> Option A
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
Step 1: Understand the selection
The code selects columns 'City' and 'Age' in that order from the DataFrame.
Step 2: Check the output order and values
The output DataFrame has columns 'City' then 'Age' with values from original data.
Final Answer:
City Age
0 NY 25
1 LA 30 -> Option C
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
Step 1: Analyze the indexing syntax
Using single brackets with multiple comma-separated strings creates a tuple key, not a list.
Since ('A', 'B') is a tuple, pandas looks for a column with that exact tuple name, which does not exist, causing KeyError.
Final Answer:
It raises a KeyError because tuple ('A', 'B') is not a column -> Option A
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?