Bird
Raised Fist0
Pandasdata~10 mins

Multiple conditions with & and | in Pandas - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Multiple conditions with & and |
Start with DataFrame
↓
Define Condition 1
↓
Define Condition 2
↓
Combine Conditions with & or |
↓
Apply combined condition to filter DataFrame
↓
Get filtered result
We start with a DataFrame, define two conditions, combine them using & (and) or | (or), then filter the DataFrame based on the combined condition.
Execution Sample
Pandas
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]})

cond = (df['A'] > 1) & (df['B'] < 8)

result = df[cond]
This code filters rows where column A is greater than 1 AND column B is less than 8.
Execution Table
StepCondition 1 (A > 1)Condition 2 (B < 8)Combined Condition ( & )Filtered Rows (Indexes)
1[False, True, True, True][True, True, True, False][False, True, True, False][1, 2]
2N/AN/AN/AFiltering complete
💡 All rows evaluated; filtered rows are those where both conditions are True.
Variable Tracker
VariableStartAfter Step 1Final
df['A'] > 1N/A[False, True, True, True][False, True, True, True]
df['B'] < 8N/A[True, True, True, False][True, True, True, False]
condN/A[False, True, True, False][False, True, True, False]
resultN/AN/ARows with index 1 and 2
Key Moments - 2 Insights
Why do we need parentheses around each condition when using & or |?
Parentheses ensure each condition is evaluated first before combining. Without them, Python's operator precedence causes errors or wrong results, as shown in the execution_table step 1.
Why can't we use 'and' or 'or' instead of '&' or '|' for combining conditions?
'and' and 'or' do not work element-wise on pandas Series. We must use '&' for AND and '|' for OR to combine boolean Series element-wise, as shown in the combined condition column.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 1. What is the combined condition value for the row with index 3?
AFalse
BTrue
CNone
DError
💡 Hint
Check the combined condition column in row 1 of execution_table for index 3.
At which step do we get the filtered rows based on the combined condition?
ABefore Step 1
BStep 2
CStep 1
DNo filtering happens
💡 Hint
Look at the 'Filtered Rows' column in execution_table step 1.
If we replace '&' with '|' in the combined condition, how would the filtered rows change?
AOnly rows where both conditions are True
BRows where either condition is True
CNo rows will be selected
DAll rows will be selected
💡 Hint
Recall that '|' means OR, so any row with True in either condition passes.
Concept Snapshot
Multiple conditions in pandas use & (and) and | (or) operators.
Each condition must be in parentheses.
Conditions combine element-wise on Series.
Use combined condition to filter DataFrame rows.
Example: df[(cond1) & (cond2)] filters rows where both cond1 and cond2 are True.
Full Transcript
This visual execution shows how to filter pandas DataFrame rows using multiple conditions combined with & and |. We start with a DataFrame and define two conditions on columns. Each condition creates a boolean Series. We combine these Series with & (and) or | (or) operators, which work element-wise. Parentheses around each condition are required to ensure correct evaluation order. The combined condition is then used to select rows from the DataFrame. The execution table traces the boolean values for each condition and the combined result for each row. Key moments clarify why parentheses and & or | are necessary instead of and/or. The quiz tests understanding of combined condition values and filtering results. This method is essential for selecting data based on multiple criteria in pandas.

Practice

(1/5)
1. Which operator should you use in pandas to select rows where age is greater than 30 and salary is above 50000?
easy
A. Use and to combine the two conditions without parentheses.
B. Use | to combine the two conditions without parentheses.
C. Use & to combine the two conditions with parentheses around each.
D. Use or to combine the two conditions with parentheses.

Solution

  1. Step 1: Understand the condition type

    We want rows where both conditions are true, so this is an AND condition.
  2. Step 2: Choose the correct pandas operator

    In pandas, & is used for AND between conditions, and each condition must be in parentheses.
  3. Final Answer:

    Use & to combine the two conditions with parentheses around each. -> Option C
  4. Quick Check:

    AND condition = & with parentheses [OK]
Hint: Use & with parentheses for AND conditions in pandas [OK]
Common Mistakes:
  • Using 'and' instead of & causes errors.
  • Not wrapping each condition in parentheses.
  • Using | instead of & for AND conditions.
2. Which of the following is the correct syntax to filter a DataFrame df for rows where df['A'] > 10 or df['B'] < 5?
easy
A. df[(df['A'] > 10) | (df['B'] < 5)]
B. df[(df['A'] > 10) & (df['B'] < 5)]
C. df[df['A'] > 10 | df['B'] < 5]
D. df[df['A'] > 10 or df['B'] < 5]

Solution

  1. Step 1: Identify the condition type

    The question asks for rows where either condition is true, so this is an OR condition.
  2. Step 2: Use correct pandas syntax for OR

    In pandas, | is used for OR, and each condition must be wrapped in parentheses inside the DataFrame filter.
  3. Final Answer:

    df[(df['A'] > 10) | (df['B'] < 5)] -> Option A
  4. Quick Check:

    OR condition = | with parentheses [OK]
Hint: Use | with parentheses for OR conditions in pandas [OK]
Common Mistakes:
  • Using & instead of | for OR conditions.
  • Not wrapping conditions in parentheses.
  • Using Python 'or' instead of | causes errors.
3. Given the DataFrame df below:
import pandas as pd
df = pd.DataFrame({'age': [25, 40, 35, 28], 'salary': [40000, 60000, 50000, 45000]})
filtered = df[(df['age'] > 30) & (df['salary'] >= 50000)]
print(filtered)

What will be the output?
medium
A. Rows with age > 30 or salary >= 50000: rows 1, 2, and 3
B. Rows with age > 30 and salary >= 50000: rows 1 and 2
C. Rows with age > 30 and salary >= 50000: row 1 only
D. Empty DataFrame

Solution

  1. Step 1: Check each row against conditions

    Row 0: age=25 (no), salary=40000 (no)
    Row 1: age=40 (yes), salary=60000 (yes)
    Row 2: age=35 (yes), salary=50000 (yes)
    Row 3: age=28 (no), salary=45000 (no)
  2. Step 2: Select rows where both conditions are true

    Rows 1 and 2 satisfy both conditions.
  3. Final Answer:

    Rows with age > 30 and salary >= 50000: rows 1 and 2 -> Option B
  4. 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

  1. Step 1: Identify the syntax error

    The second condition df['age'] < 30 is not wrapped in parentheses, which is required when using &.
  2. 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)]
  3. Final Answer:

    Add parentheses around the second condition: df[(df['score'] > 80) & (df['age'] < 30)] -> Option D
  4. 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?
hard
A. df[((df['temperature'] >= 20) & (df['temperature'] <= 30)) | (df['humidity'] < 40)]
B. df[(df['temperature'] >= 20) & df['temperature'] <= 30 | (df['humidity'] < 40)]
C. df[(df['temperature'] >= 20) | (df['temperature'] <= 30) & (df['humidity'] < 40)]
D. df[(df['temperature'] >= 20) & (df['humidity'] < 40) | (df['temperature'] <= 30)]

Solution

  1. Step 1: Understand the condition logic

    We want rows where temperature is between 20 and 30 (both conditions combined with AND), OR humidity is below 40.
  2. Step 2: Apply correct operator precedence with parentheses

    Use parentheses to group the temperature range conditions with & and then combine with | for humidity condition.
  3. Final Answer:

    df[((df['temperature'] >= 20) & (df['temperature'] <= 30)) | (df['humidity'] < 40)] -> Option A
  4. Quick Check:

    Group AND conditions, then OR with other condition [OK]
Hint: Group AND conditions in parentheses before OR [OK]
Common Mistakes:
  • Missing parentheses causing wrong logic.
  • Mixing & and | without grouping.
  • Using | inside AND group incorrectly.
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard