🧮 Pandas Binary Operations & Boolean Indexing – Compare, Filter, and Compute Like a Pro
🧲 Introduction – Why Use Binary Operations & Boolean Indexing?
Pandas supports binary operations (like addition, comparison, and logical operations) and Boolean indexing for powerful, element-wise computation and conditional filtering. These tools allow you to quickly analyze, transform, and filter data without loops—making your workflow efficient and readable.
🎯 In this guide, you’ll learn:
- Use binary operators: arithmetic & comparison
- Filter data using Boolean masks
- Chain multiple conditions with logical operators
- Use
where()
,mask()
, andquery()
for advanced Boolean logic
🔢 1. Sample DataFrame for Practice
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [25, 32, 18, 45],
'Score': [88, 92, 67, 75]
})
➕ 2. Binary Arithmetic Operations
df['Bonus'] = df['Score'] + 10
✔️ Adds 10 to each value in the 'Score'
column.
Other element-wise operations:
df['DoubleAge'] = df['Age'] * 2
df['ScoreDiff'] = df['Score'] - df['Age']
df['Normalized'] = df['Score'] / df['Score'].max()
🔍 3. Binary Comparison Operations
df['IsAdult'] = df['Age'] >= 18
df['HighScorer'] = df['Score'] > 90
✔️ Creates Boolean columns based on conditions.
👉 Output:
Name Age Score IsAdult HighScorer
0 Alice 25 88 True False
1 Bob 32 92 True True
2 Charlie 18 67 True False
3 David 45 75 True False
🎯 4. Boolean Indexing for Filtering Rows
df[df['Score'] > 80]
✔️ Returns rows where the condition is True
.
Chain Multiple Conditions (use &
, |
, ~
)
df[(df['Age'] > 20) & (df['Score'] < 90)]
✔️ Use parentheses with logical operators:
&
for AND|
for OR~
for NOT
🎛️ 5. Use .where()
for Conditional Replacement
df['Status'] = df['Score'].where(df['Score'] >= 70, 'Fail')
✔️ Keeps score if condition is True
, replaces with 'Fail'
otherwise.
👉 Use .mask()
for the inverse behavior:
df['Masked'] = df['Score'].mask(df['Score'] < 70, 'Low')
🧮 6. Element-wise Binary Operations Between DataFrames
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})
df_sum = df1 + df2
✔️ Adds DataFrames element-wise (aligned by index and columns).
🔎 7. Using .query()
for SQL-like Filtering
df.query('Age > 30 and Score < 90')
✔️ Clean and readable alternative for Boolean filters.
📌 Summary – Key Takeaways
Binary operations and Boolean indexing are cornerstones of Pandas for data analysis and transformation. They allow you to efficiently compute values, compare columns, and filter rows with minimal code.
🔍 Key Takeaways:
- Use
+
,-
,*
,/
,==
,>
,<
for binary operations - Filter data using
df[condition]
- Combine multiple conditions with
&
,|
,~
- Use
.where()
and.mask()
for conditional assignment - Use
.query()
for readable logic filters
⚙️ Real-world relevance: Used in data cleaning, feature selection, conditional transformations, and exploratory data analysis (EDA).
❓ FAQs – Binary Operations & Boolean Indexing in Pandas
❓ Why do I need parentheses in combined conditions?
Logical operators (&
, |
) need parentheses due to Python’s operator precedence.
❓ What’s the difference between .where()
and np.where()
?
.where()
→ keeps DataFrame structurenp.where()
→ returns array or can be used in assignments
❓ Can I compare two columns directly?
Yes:
df['Score'] > df['Age']
❓ What happens when indexes don’t align in binary operations between DataFrames?
Pandas fills unmatched labels with NaN
.
❓ Is .query()
better than Boolean indexing?
✅ It’s more readable for complex filters, especially with many conditions.
Share Now :