Recall & Review
beginner
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?✗ Incorrect
Using
df['age'] selects the 'age' column as a Series.How do you select multiple columns 'name' and 'salary' from a DataFrame
df?✗ 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?✗ 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?
✗ Incorrect
Using
df['class'] avoids conflicts with DataFrame methods.How can you select all columns starting with 'A' in a DataFrame
df?✗ 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.