What is the simplest way to select a single column from a DataFrame in pandas?
You can select a single column by using the column name in square brackets, like df['column_name']. This returns a Series with the data from that column.
Click to reveal answer
beginner
How do you select multiple columns from a DataFrame?
Use a list of column names inside double square brackets, like df[['col1', 'col2']]. This returns a new DataFrame with only those columns.
Click to reveal answer
beginner
What happens if you try to select a column that does not exist in the DataFrame?
Pandas will raise a KeyError because the column name is not found in the DataFrame's columns.
Click to reveal answer
intermediate
Can you select columns using attribute-style access like df.column_name? When is it recommended?
Yes, you can use df.column_name if the column name is a valid Python identifier and does not conflict with DataFrame methods. However, it is safer to use df['column_name'] to avoid errors.
Click to reveal answer
intermediate
How can you select columns based on a condition, for example, all columns with names starting with 'A'?
You can use list comprehension or the filter method. For example, df[[col for col in df.columns if col.startswith('A')]] selects all columns starting with 'A'.
Click to reveal answer
Which syntax selects a single column named 'age' from a DataFrame df?
Adf['age']
Bdf[['age']]
Cdf.age()
Ddf.get('age')
✗ Incorrect
Using df['age'] selects the 'age' column as a Series.
How do you select multiple columns 'name' and 'salary' from a DataFrame df?
Adf['name', 'salary']
Bdf[['name', 'salary']]
Cdf.name.salary
Ddf.get(['name', 'salary'])
✗ Incorrect
Use double square brackets with a list of column names: df[['name', 'salary']].
What error occurs if you try to select a non-existent column 'height' from df?
AKeyError
BValueError
CTypeError
DIndexError
✗ Incorrect
Pandas raises a KeyError when the column name is not found.
Which method is safest to select a column named 'class' that conflicts with DataFrame methods?
Adf.class()
Bdf.class
Cdf['class']
Ddf.get_class()
✗ Incorrect
Using df['class'] avoids conflicts with DataFrame methods.
How can you select all columns starting with 'A' in a DataFrame df?
Adf.filter(regex='^A', axis=1)
Bdf[[col for col in df.columns if col.startswith('A')]]
Cdf.select('A*')
DBoth A and B
✗ Incorrect
Both df.filter(regex='^A', axis=1) and list comprehension work to select columns starting with 'A'.
Explain how to select a single column and multiple columns from a pandas DataFrame.
Think about the difference between single and double square brackets.
You got /2 concepts.
Describe how to safely select columns when column names might conflict with DataFrame methods or are not valid Python identifiers.
Consider what happens if a column name is a Python keyword.
You got /2 concepts.
Practice
(1/5)
1. Which of the following is the correct way to select a single column named 'Age' from a DataFrame df?
easy
A. df[['Age']]
B. df.select('Age')
C. df.Age()
D. df['Age']
Solution
Step 1: Understand single column selection syntax
To select one column from a DataFrame, use df['column_name'].
Step 2: Check each option
df['Age'] uses df['Age'], which is correct. df[['Age']] returns a DataFrame, not a Series. df.Age() uses parentheses incorrectly. df.select('Age') is not a valid pandas method.
Final Answer:
df['Age'] -> Option D
Quick Check:
Single column selection = df['Age'] [OK]
Hint: Use single brackets for one column, double for multiple [OK]
Common Mistakes:
Using double brackets for single column returns DataFrame, not Series
Using dot notation with parentheses like a function
Trying non-existent methods like select()
2. Which syntax correctly selects multiple columns 'Name' and 'Salary' from DataFrame df?
easy
A. df['Name', 'Salary']
B. df[['Name', 'Salary']]
C. df.Name, df.Salary
D. df.select(['Name', 'Salary'])
Solution
Step 1: Recall syntax for selecting multiple columns
Use double brackets with a list of column names: df[['col1', 'col2']].
Step 2: Evaluate options
df[['Name', 'Salary']] matches the correct syntax. df['Name', 'Salary'] is invalid syntax. df.Name, df.Salary selects columns separately, not together. df.select(['Name', 'Salary']) is not a pandas method.
Final Answer:
df[['Name', 'Salary']] -> Option B
Quick Check:
Multiple columns = df[['Name', 'Salary']] [OK]
Hint: Use double brackets with list for multiple columns [OK]
Common Mistakes:
Using single brackets with multiple column names separated by commas
Trying to select multiple columns with dot notation
Using non-existent select() method
3. Given the DataFrame df below:
Name Age Salary
0 John 28 50000
1 Anna 22 60000
2 Mike 32 55000
What is the output of df[['Name', 'Age']]?
medium
A.
Name Age
0 John 28
1 Anna 22
2 Mike 32
B.
Name Age Salary
0 John 28 50000
1 Anna 22 60000
2 Mike 32 55000
C.
0 John
1 Anna
2 Mike
Name: Name, dtype: object
D.
28
22
32
Solution
Step 1: Understand what df[['Name', 'Age']] does
This selects only the columns 'Name' and 'Age' from the DataFrame, returning a smaller DataFrame with those columns.
Step 2: Match output to options
Name Age
0 John 28
1 Anna 22
2 Mike 32
shows only 'Name' and 'Age' columns with all rows, which matches the expected output.
Name Age Salary
0 John 28 50000
1 Anna 22 60000
2 Mike 32 55000
includes 'Salary' which is not selected.
0 John
1 Anna
2 Mike
Name: Name, dtype: object
shows only the 'Name' column as a Series.
28
22
32
shows only ages as a list, which is incorrect.
Final Answer:
DataFrame with columns 'Name' and 'Age' only -> Option A
Quick Check:
Selected columns = 'Name' and 'Age' only [OK]
Hint: Double brackets return DataFrame with selected columns [OK]
Common Mistakes:
Expecting single brackets to return multiple columns
Confusing Series output with DataFrame output
Including unselected columns in output
4. What is wrong with this code to select the column 'Score' from DataFrame df?
df.Score()
medium
A. Parentheses cause an error; dot notation should be without ()
B. Column names cannot have uppercase letters
C. Column name must be in double brackets
D. Dot notation cannot be used to select columns
Solution
Step 1: Understand dot notation for column selection
Dot notation selects columns as attributes without parentheses, like df.Score.
Step 2: Identify error in code
Using parentheses () after Score tries to call it like a function, causing an error.
Final Answer:
Parentheses cause an error; dot notation should be without () -> Option A
Quick Check:
Dot notation = df.Score without parentheses [OK]
Hint: Dot notation is attribute access, no parentheses [OK]
Common Mistakes:
Adding parentheses after column name with dot notation
Thinking dot notation works for all column names
Using double brackets with dot notation
5. You have a DataFrame df with columns 'A', 'B', 'C', and 'D'. You want to create a new DataFrame df_new that contains only columns 'B' and 'D' but in reverse order ('D' first, then 'B'). Which code correctly does this?
hard
A. df_new = df['D', 'B']
B. df_new = df[['B', 'D']]
C. df_new = df[['D', 'B']]
D. df_new = df[['B', 'D']].reindex(columns=['B', 'D'])
Solution
Step 1: Understand column selection order
The order of columns in the list inside double brackets determines the order in the new DataFrame.
Step 2: Check each option
df_new = df[['D', 'B']] selects columns 'D' then 'B', which matches the requirement. df_new = df[['B', 'D']] selects 'B' then 'D', wrong order. df_new = df['D', 'B'] is invalid tuple syntax inside single brackets. df_new = df[['B', 'D']].reindex(columns=['B', 'D']) reindexes to the same order 'B' then 'D', incorrect.
Final Answer:
df_new = df[['D', 'B']] -> Option C
Quick Check:
Order columns by list inside double brackets [OK]
Hint: Order columns in list to set output order [OK]
Common Mistakes:
Selecting columns in wrong order
Using reindex but specifying the original order
Using tuple inside single brackets for multiple columns
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.