`isin` tests each value in a pandas Series or DataFrame column against a collection of candidates. It returns Boolean results: `True` for a match and `False` otherwise. Use that Boolean Series as a row selector when a question means “one of these values,” rather than writing several equality comparisons joined with `|`. section 13.3 section 12.10
A common pattern is `df.loc[df['column'].isin([...])]`. The expression inside `loc` determines which rows match; `loc` can then retrieve those rows or target a particular column for assignment. For example, select rows whose `hour` is 3 or 6 and replace their `temp` values with `NaN` before interpolation. section 2.22
Filter graduate programs
To retain programs whose credential is either a master’s or doctoral degree, test the `CREDDESC` column against both labels: `fields_of_study_df.loc[fields_of_study_df['CREDDESC'].isin(["Master's Degree", "Doctoral Degree"])]` The filtered rows can then be grouped by institution, counted, and plotted. The membership test keeps the degree criterion separate from the later aggregation question. section 13.3
Membership results also support comparisons between two Series:
- Use `ug_schools.isin(grad_schools)` to identify undergraduate-school entries that also occur among graduate-school entries. section 13.3
- Negate the Boolean result with `~` to find entries absent from the comparison Series, then use `drop_duplicates()` and `size` to count distinct schools. section 13.3
- For a short list of alternatives, prefer one `isin` membership test to repeated `==` comparisons joined by `|`; measure performance on the actual query rather than assuming one approach always wins. section 12.10 section 12.9
(
fields_of_study_df
.loc[fields_of_study_df['CREDDESC']
.isin(["Master's Degree", "Doctoral Degree"])]
)
This listing shows the complete filtering expression for two credential labels, ready to extend with grouping and plotting. section 13.3
`isin` is especially useful when the allowed values are already collected in a Python list or another Series. It expresses the data question directly: keep matching categories, or invert the mask to investigate what is missing from the other collection. section 12.10 section 13.3
