Boolean Mask

noun

say it: “BOO-lee-un MASK” /ˈbuliən ˈmæsk/

A column of True and False values, one per row, that pandas uses to keep only the rows where the answer is True.

What is a Boolean mask in pandas?

A Boolean mask is a column of True and False answers, one for each row, made by a comparison such as products["price"] < 100. Putting it in square brackets keeps only the rows where it's True.

import pandas as pd
products = pd.DataFrame({"name": ["Trail Boot", "Rain Jacket", "Camp Stove"], "price": [129.0, 89.0, 45.0]})
cheap = products["price"] < 100            # False, True, True
print(products[cheap]["name"].tolist())    # ['Rain Jacket', 'Camp Stove']

In a sentence

  • “Counting the Trues in the Boolean mask told her how many jackets were in stock.”
  • “The Boolean mask marked every product under $100 as True.”

Level: Journeyman · More Python terms at this level

Updated