What is the purpose of selecting rows by condition in pandas?
Selecting rows by condition helps you filter data to keep only the rows that meet specific criteria, making it easier to analyze relevant information.
Click to reveal answer
beginner
How do you select rows where a column 'Age' is greater than 30 in a pandas DataFrame named df?
Use df[df['Age'] > 30] to get all rows where the 'Age' column value is greater than 30.
Click to reveal answer
beginner
What does the expression df[df['Score'] >= 80] return?
It returns a new DataFrame containing only the rows where the 'Score' column is 80 or higher.
Click to reveal answer
intermediate
How can you select rows where column 'City' is either 'New York' or 'Chicago'?
Use df[df['City'].isin(['New York', 'Chicago'])] to select rows where 'City' is one of the listed values.
Click to reveal answer
intermediate
What is the difference between using & and | operators in pandas conditions?
& means AND (both conditions must be true), | means OR (at least one condition must be true). Remember to use parentheses around each condition.
Click to reveal answer
Which code selects rows where 'Salary' is less than 50000?
Adf.loc['Salary' < 50000]
Bdf['Salary'] < 50000
Cdf[df['Salary'] < 50000]
Ddf['Salary'].filter(<50000)
✗ Incorrect
df[df['Salary'] < 50000] filters the DataFrame to keep rows where 'Salary' is less than 50000.
How do you select rows where 'Age' is between 20 and 30 inclusive?
Adf[(df['Age'] >= 20) & (df['Age'] <= 30)]
Bdf[df['Age'] > 20 and df['Age'] < 30]
Cdf[df['Age'] >= 20 | df['Age'] <= 30]
Ddf[df['Age'].between(20, 30)]
✗ Incorrect
Use & with parentheses: df[(df['Age'] >= 20) & (df['Age'] <= 30)] or use df[df['Age'].between(20, 30)] to select rows where Age is between 20 and 30.
What does df[df['Status'] == 'Active'] do?
ASelects rows where 'Status' is 'Active'
BSelects rows where 'Status' is not 'Active'
CChanges 'Status' to 'Active'
DDeletes rows where 'Status' is 'Active'
✗ Incorrect
It filters the DataFrame to keep only rows where the 'Status' column equals 'Active'.
Which method helps select rows where a column matches any value in a list?
Afilter()
Bisin()
Cmatch()
Dcontains()
✗ Incorrect
The isin() method checks if values in a column are in a list of values.
How do you combine two conditions to select rows where 'Age' > 25 OR 'Score' > 90?
Adf[(df['Age'] > 25) & (df['Score'] > 90)]
Bdf[df['Age'] > 25 and df['Score'] > 90]
Cdf[df['Age'] > 25 or df['Score'] > 90]
Ddf[(df['Age'] > 25) | (df['Score'] > 90)]
✗ Incorrect
Use | for OR with parentheses: df[(df['Age'] > 25) | (df['Score'] > 90)].
Explain how to select rows in a pandas DataFrame based on a condition involving a single column.
Think about how you write conditions like 'Age > 30' inside the brackets.
You got /3 concepts.
Describe how to combine multiple conditions to filter rows in pandas.
Remember to use parentheses and the right logical operators.
You got /3 concepts.
Practice
(1/5)
1. What does the expression df[df['age'] > 30] do in pandas?
easy
A. Deletes rows where the 'age' column is greater than 30
B. Selects columns where the 'age' value is greater than 30
C. Selects rows where the 'age' column is greater than 30
D. Sorts the DataFrame by the 'age' column
Solution
Step 1: Understand the condition inside the brackets
The condition df['age'] > 30 creates a True/False mask for each row based on the 'age' value.
Step 2: Apply the condition to select rows
Using df[condition] returns only rows where the condition is True, i.e., 'age' > 30.
Final Answer:
Selects rows where the 'age' column is greater than 30 -> Option C
Quick Check:
Condition filters rows [OK]
Hint: Use df[condition] to filter rows by True/False mask [OK]
Common Mistakes:
Thinking it selects columns instead of rows
Confusing filtering with sorting
Using parentheses incorrectly around conditions
2. Which of the following is the correct syntax to select rows where column 'score' equals 100?
easy
A. df[df.score = 100]
B. df[df['score'] = 100]
C. df[df['score'] === 100]
D. df[df['score'] == 100]
Solution
Step 1: Identify correct equality operator in pandas
In pandas, to compare values, use double equals ==, not single = or triple ===.
Step 2: Check correct column access syntax
Using df['score'] is correct and safe for all column names.
Final Answer:
df[df['score'] == 100] -> Option D
Quick Check:
Use == for comparison and brackets for column [OK]
Hint: Use df[df['col'] == value] with double equals for condition [OK]
Common Mistakes:
Using single = instead of == for comparison
Using triple === which is invalid in Python
Accessing columns without quotes when name has special chars
3. Given the DataFrame:
import pandas as pd
data = {'name': ['Ann', 'Bob', 'Cara', 'Dan'], 'age': [25, 35, 30, 40]}
df = pd.DataFrame(data)
What is the output of df[df['age'] >= 30]?
medium
A. Rows with names Bob, Cara, and Dan
B. Rows with names Ann and Bob
C. Rows with names Ann and Cara
D. Rows with names Dan only
Solution
Step 1: Evaluate the condition df['age'] >= 30
This condition is True for ages 35 (Bob), 30 (Cara), and 40 (Dan).
Step 2: Select rows where condition is True
Rows with names Bob, Cara, and Dan are returned.
Final Answer:
Rows with names Bob, Cara, and Dan -> Option A
Quick Check:
age >= 30 selects Bob, Cara, Dan [OK]
Hint: Check condition True rows, then list their names [OK]
Common Mistakes:
Including Ann who is 25 (less than 30)
Confusing >= with >
Misreading the DataFrame data
4. What is wrong with this code to select rows where 'score' is above 50?
df[df['score'] > 50 && df['age'] < 30]
medium
A. Using && instead of & for combining conditions
B. Missing parentheses around each condition
C. Using single & instead of &&
D. Using | instead of & for and condition
Solution
Step 1: Check operator for combining conditions in pandas
In pandas, use single ampersand & for 'and', not double ampersand &&.
Step 2: Confirm correct syntax
Correct syntax is df[(df['score'] > 50) & (df['age'] < 30)] with parentheses and single &.
Final Answer:
Using && instead of & for combining conditions -> Option A
Quick Check:
Use & not && for and in pandas [OK]
Hint: Use & for and, | for or, with parentheses [OK]
Common Mistakes:
Using && which causes syntax error
Not adding parentheses around each condition
Using | instead of & for and
5. You have a DataFrame df with columns 'city', 'temperature', and 'humidity'. How do you select rows where the city is 'Paris' and temperature is above 20 or humidity is below 30?