liveBook · Manning

Boolean indexing

As explained in Pandas Workout: 200 exercises to make you a stronger data analyst

Boolean-mask indexing selects data elements at positions where a Boolean condition is true.

Boolean indexing, also called mask indexing, filters a pandas Series or DataFrame with a same-shaped sequence of True and False values. Think of the Boolean values as a sieve: True keeps the corresponding item, while False leaves it out. A mask selects data; it does not transform the selected values. section 1.13

In practice, write the condition inside .loc. pandas evaluates that condition first, producing a Boolean Series, then uses it to select matching rows. For a Series, `s.loc[s < 30]` returns only values below 30. A condition can also compare values with a calculated result, such as `s.loc[s <= s.mean()]`. section 1.13

Find products selling above average

Start with the question, then make the condition describe the rows you want. `df['sales'] > df['sales'].mean()` produces one Boolean value per row. Pass it as the first argument to `.loc`; pass desired columns after the comma: `df.loc[df['sales'] > df['sales'].mean(), ['product_id', 'name']]` This separates row filtering from column selection. section 2.14

For DataFrames, `.loc[row_mask, column_selector]` makes the two axes explicit. The mask before the comma chooses rows; a second Boolean mask after the comma can choose columns. The same pattern supports assignment: a scalar is broadcast to every match, while a list, array, or Series must match the selected shape. section 2.8

Count unusual taxi rides

Filter before aggregating to answer a focused question. To count rides with more than eight passengers, select the matching `passenger_count` values and call `count()`: `df.loc[df['passenger_count'] > 8, 'passenger_count'].count()` Combine conditions with `&`, keeping each comparison in parentheses: `(df['payment_type'] == 2) & (df['total_amount'] > 1000)`. section 3.2

Masks are especially useful when a condition can match more than one row. To find every minimum or maximum closing value, compare the column against its aggregate: `df.loc[df['value'] == df['value'].min(), ['date', 'value']]`. This retains ties rather than assuming one location. section 3.17 section 1.15

Use Boolean indexing in cleaning pipelines, too. A string test such as `df['age'].str.isdigit()` identifies rows whose values contain only digits, allowing those rows to be retained before conversion. After numeric conversion, a range condition such as `df.loc[df['age'] < 120]` removes implausible values. section 5.5 section 5.6

Figure 2.9 Graphical depiction of df.loc[df['sales'] > df['sales'].mean(), ['product_id', 'name']]

This diagram makes the two parts of `.loc` visible: the Boolean row condition filters records, and the column list narrows the returned fields. section 2.14

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

How deep it goes
Explains Boolean Series creation, .loc selection across both axes, compound conditions, and shape-aware assignment.
Examples
Uses numeric Series, product sales, taxi rides, market prices, and dirty celebrity-age strings.
What it assumes
Assumes familiarity with pandas Series, DataFrames, indexes, and .loc while explaining Boolean masks from explicit True and False values.
What you can do after
Apply it via pandas .loc to filter selected rows and columns, count matches, and update matching values.
How it connects
Connects .loc, Series, DataFrame, comparison operators, broadcasting, aggregation, count, query, and string methods.

Also explained in

Compare how 2 books explain boolean indexing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.