0
0
Pandasdata~10 mins

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

Choose your learning style9 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.