Filtering

Filtering: Filtering Rows with a Condition, Combining Conditions with &, | and ~.

Updated

  1. Filtering Rows with a Condition (Apprentice): A comparison such as products["price"] < 100 gives a True or False answer for every row, called a mask; putting the mask in square brackets, products[mask], keeps only the rows where it is True; and sum() and mean() on the mask count the matching rows and give their share of the table.
  2. Combining Conditions with &, | and ~ (Apprentice): Python's and, or and not can't combine whole columns of True and False, so pandas uses & (and), | (or) and ~ (not), with parentheses around each comparison; isin, between and str.contains replace long chains of conditions, and query writes a filter as text.