Skip to content

Non ancora disponibile in italiano. Vedi tutto ciò che è disponibile in italiano

Stockertim Dictionary · Technology

Boolean mask

Also called boolean indexing, boolean filter, True/False mask

In short

A column of True and False values, one per row, built from a condition and used to keep only the rows where the condition is true.

  • Beginner
  • Concept
  • Checked

In detail

A Boolean mask is how pandas filters rows. Writing a condition about a column, such as df['amount'] > 1000, does not filter anything by itself; it returns a new column of True and False values, one for each row. Putting that column inside square brackets, df[df['amount'] > 1000], keeps the rows marked True and drops the others.

Masks can be combined. The symbol & means and, | means or and ~ means not, and each condition must be wrapped in its own brackets because of the order in which Python applies the symbols: df[(df['region'] == 'North') & (df['amount'] > 1000)]. The plain words and and or do not work on whole columns and raise an error.

Helper methods produce masks for common tests. isin(['A', 'B']) checks membership of a list, between(10, 20) checks a range, str.contains('bank') checks text and isna() finds missing values. Because True counts as 1 and False as 0, mask.sum() gives the number of matching rows and mask.mean() the share, which is a quick way to size a filter before using it.

The usual mistakes are quiet ones. A comparison with a missing value gives False, so rows with gaps vanish from both a filter and its opposite. Text comparisons are case-sensitive. A mask built from one table applied to another table with different row labels can align wrongly or raise an error, so masks are best built and used on the same DataFrame in one step.

Worked example

A water utility in Lusaka has 10,000 meter readings and wants accounts with usage above 50 cubic metres.

  1. mask = df['usage_m3'] > 50 creates a True/False column of 10,000 values.
  2. mask.sum() returns 400, the number of True values.
  3. Share of accounts flagged: 400 / 10,000 = 4%

df[mask] returns the 400 high-usage accounts for inspection, and the count is known before the list is sent out.

In practice

Analysts use masks to select a period, a region or a product, to find rows breaking a data rule and to split data into groups for comparison. The same idea exists in spreadsheets as a helper column of TRUE and FALSE used with FILTER or an AutoFilter.

Watch out

  • Use & and | with brackets around each condition; and and or raise an error on columns.
  • Rows with missing values are False in a condition and in its opposite, so they can fall out of both halves of a split.

Learn it properly

Official sources

Facts last checked on 9 November 2026.

Education, not advice: the Stockertim Dictionary explains what terms mean. It never recommends buying or selling anything.

English