0
0
Data Analysis Pythondata~5 mins

Boolean filtering in Data Analysis Python

Choose your learning style9 modes available
Introduction

Boolean filtering helps you pick only the data you want by using true or false conditions.

You want to find all customers who spent more than $100.
You need to select rows where the temperature is below freezing.
You want to see only products that are in stock.
You want to filter survey responses where people answered 'Yes'.
You want to analyze data only for a specific month or year.
Syntax
Data Analysis Python
filtered_data = data[condition]

data is your dataset, like a table or DataFrame.

condition is a true/false check for each row.

Examples
Selects rows where the age column is greater than 30.
Data Analysis Python
filtered = df[df['age'] > 30]
Selects rows where score is at least 80 and passed is True.
Data Analysis Python
filtered = df[(df['score'] >= 80) & (df['passed'] == True)]
Selects rows where the city is exactly 'New York'.
Data Analysis Python
filtered = df[df['city'] == 'New York']
Sample Program

This code creates a small table of people with their ages and scores. Then it picks only the people older than 30 and shows their data.

Data Analysis Python
import pandas as pd

# Create a simple data table
data = pd.DataFrame({
    'name': ['Alice', 'Bob', 'Charlie', 'David'],
    'age': [25, 35, 30, 40],
    'score': [85, 90, 70, 60]
})

# Filter rows where age is greater than 30
filtered_data = data[data['age'] > 30]

print(filtered_data)
OutputSuccess
Important Notes

Use parentheses around multiple conditions when combining with & (and) or | (or).

Boolean filtering does not change the original data unless you save the filtered result.

Make sure your condition returns a series of True/False values matching the data rows.

Summary

Boolean filtering helps select rows based on true/false checks.

Use conditions inside square brackets after your data.

Combine multiple conditions with & (and) or | (or), using parentheses.