Boolean Masking
A boolean mask selects rows marked True. A comparison on a column usually produces a Series with the same index as its source. For a Series mask, pandas matches index labels, not the current position of each value. A mask from another table must cover the target labels and refer to the same entities; missing labels can raise an indexing error. A nullable boolean mask may also contain NA, which is excluded during selection.
Combine Series masks with &, |, and ~, enclosing each comparison in parentheses. Python boolean operators ask for one truth value rather than a value per row. Using and or or on a Series raises an ambiguous-truth-value error. Missing parentheses can instead produce a TypeError depending on the operands; the error wording alone does not identify the cause.
Missing comparisons depend on the dtype and operator. With floating-point NaN, >= 60 is False but != 60 is True; nullable Int64 comparisons can produce pd.NA. Filtering alone therefore does not distinguish unknown values from measured values that fail a condition. Use isna() or notna() to identify and report missing observations before choosing a filter policy.
Filtering returns a new object rather than a window onto the original, so writing through a filter in one chained expression changes nothing. To modify the original, name rows and column together in a single loc assignment; to work on the subset, take an explicit copy. Beyond filtering, the same masks feed conditional assignment and grouped counts, which is why they are the most reused idea in everyday pandas work.
References: pandas boolean indexing, pandas missing data. See it in use in Pandas Foundations: Tables, Filtering, Grouping, and Joins.
Discover more from Insightful Data Lab
Subscribe to get the latest posts sent to your email.
