Step 2: Select rows where both conditions are true
Rows 1 and 2 satisfy both conditions.
Final Answer:
Rows with age > 30 and salary >= 50000: rows 1 and 2 -> Option B
Quick Check:
Both conditions true for rows 1 and 2 [OK]
Hint: Check each condition row-wise, then combine with & [OK]
Common Mistakes:
Confusing & with | and selecting wrong rows.
Ignoring parentheses causing syntax errors.
Misreading >= as > or vice versa.
4. What is wrong with this pandas filter code?
df[(df['score'] > 80) & df['age'] < 30]
Choose the best fix.
medium
A. Use 'and' instead of &.
B. Replace & with | to fix the syntax.
C. Remove parentheses around the first condition.
D. Add parentheses around the second condition: df[(df['score'] > 80) & (df['age'] < 30)]
Solution
Step 1: Identify the syntax error
The second condition df['age'] < 30 is not wrapped in parentheses, which is required when using &.
Step 2: Correct the syntax by adding parentheses
Wrap the second condition in parentheses to fix the error: df[(df['score'] > 80) & (df['age'] < 30)]
Final Answer:
Add parentheses around the second condition: df[(df['score'] > 80) & (df['age'] < 30)] -> Option D
Quick Check:
Each condition must be in parentheses with & [OK]
Hint: Wrap each condition in parentheses when using & or | [OK]
Common Mistakes:
Not using parentheses around all conditions.
Using 'and' instead of &.
Changing & to | without reason.
5. You have a DataFrame df with columns temperature and humidity. You want to select rows where temperature is between 20 and 30 inclusive, or humidity is below 40. Which of the following code snippets correctly applies these conditions?