0
0
Data Analysis Pythondata~5 mins

Boolean indexing in Data Analysis Python

Choose your learning style9 modes available
Introduction

Boolean indexing helps you pick data that meets certain true/false conditions. It makes finding and working with specific parts of your data easy.

You want to find all sales above a certain amount in a sales list.
You need to select students who passed an exam from a grade table.
You want to filter rows in a table where a date is after a certain day.
You want to quickly check which items in a list meet a condition, like prices below 10 dollars.
Syntax
Data Analysis Python
filtered_data = data[condition]

data can be a list, array, or DataFrame.

condition is a boolean array or expression that is True for items you want to keep.

Examples
This selects rows where the 'age' column is more than 25.
Data Analysis Python
import pandas as pd

# Create a DataFrame
data = pd.DataFrame({'age': [20, 25, 30, 35]})

# Select rows where age is greater than 25
filtered = data[data['age'] > 25]
print(filtered)
This picks numbers bigger than 15 from the array.
Data Analysis Python
import numpy as np

arr = np.array([10, 15, 20, 25])

# Select elements greater than 15
filtered_arr = arr[arr > 15]
print(filtered_arr)
Sample Program

This program filters the DataFrame to show only people with scores 80 or above.

Data Analysis Python
import pandas as pd

# Sample data of people and their scores
scores = pd.DataFrame({
    'name': ['Anna', 'Bob', 'Cara', 'Dan'],
    'score': [85, 62, 90, 70]
})

# Select rows where score is at least 80
high_scores = scores[scores['score'] >= 80]

print(high_scores)
OutputSuccess
Important Notes

Boolean indexing keeps the original order of data.

Make sure the condition matches the data length to avoid errors.

Summary

Boolean indexing helps select data by true/false conditions.

It works with lists, arrays, and tables like DataFrames.

Use it to quickly filter data based on your needs.