AI engineering glossary · NumPy pandas visualization
What is boolean indexing?
Boolean indexing (also called boolean mask filtering): Filtering a DataFrame or Series by passing a True/False condition, so only rows where it is true are kept. Conditions are combined with & (and), | (or) and ~ (not), each wrapped in brackets. It is the pandas counterpart of a SQL WHERE clause.
Boolean indexing: an example
df[(df['city'] == 'Pune') & (df['sales'] > 1000)] keeps Pune rows with sales above 1000.
Boolean indexing in Python
A short example you can run (Python 3.11, NumPy). The output below is what it really prints.
import numpy as np
marks = np.array([35, 78, 92, 41, 66, 88])
passed = marks >= 40
print(passed)
print(marks[passed])
print(marks[(marks > 60) & (marks < 90)])Output
[False True True True True True] [78 92 41 66 88] [78 66 88]
Related terms
More NumPy pandas visualization terms
- Boolean mask
- Box plot
- Broadcasting
- C order
- Categorical dtype
- Chunked reading
- Colormap
- Colour-blind-safe palette
Where you learn it
Boolean indexing is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.