isin()
noun
say it: “IZ-in” /ˈɪzˌɪn/
A pandas method that checks each value in a column against a list and answers True when the value is in the list.
What does isin() do in pandas?
isin() is a pandas method that answers True for each value in a column that appears in a list you give it, and
False for the rest, so one call replaces a long chain of | comparisons.
import pandas as pd
products = pd.DataFrame({"name": ["Trail Boot", "Rain Jacket", "Camp Stove"], "category": ["boots", "jackets", "stoves"]})
wanted = products["category"].isin(["boots", "jackets"])
print(products[wanted]["name"].tolist()) # ['Trail Boot', 'Rain Jacket']
In a sentence
- “With isin(), the filter for five states fit on one line.”
- “isin() kept only the rows whose category was boots or jackets.”
Level: Journeyman · More Python terms at this level
Updated