How to Use isin in pandas for Filtering Data
Use the
isin() method in pandas to check if each element in a DataFrame or Series is contained in a list or another iterable. It returns a boolean mask that you can use to filter rows matching the values you want.Syntax
The isin() method is called on a pandas Series or DataFrame column. It takes one main argument: a list, set, or iterable of values to check against. It returns a boolean Series indicating if each element is in the given list.
values: list or iterable of values to check membership- Returns: Boolean Series or DataFrame mask
python
Series.isin(values) # Example: # df['column'].isin([value1, value2, value3])
Example
This example shows how to filter rows in a DataFrame where the 'fruit' column contains only certain fruits from a list.
python
import pandas as pd data = {'fruit': ['apple', 'banana', 'cherry', 'date', 'fig'], 'quantity': [5, 3, 8, 6, 2]} df = pd.DataFrame(data) # List of fruits to keep fruits_to_keep = ['apple', 'cherry', 'fig'] # Use isin to filter rows filtered_df = df[df['fruit'].isin(fruits_to_keep)] print(filtered_df)
Output
fruit quantity
0 apple 5
2 cherry 8
4 fig 2
Common Pitfalls
One common mistake is passing a single value instead of a list or iterable to isin(). This causes unexpected results because isin() expects a list-like object.
Another pitfall is confusing isin() with equality checks. isin() checks membership in multiple values, not equality to one value.
python
import pandas as pd data = {'color': ['red', 'blue', 'green', 'blue']} df = pd.DataFrame(data) # Wrong: passing a single string instead of list wrong_filter = df['color'].isin('blue') # This checks each character # Right: pass a list with one string right_filter = df['color'].isin(['blue']) print('Wrong filter result:') print(wrong_filter) print('\nRight filter result:') print(right_filter)
Output
Wrong filter result:
0 False
1 True
2 False
3 True
Name: color, dtype: bool
Right filter result:
0 False
1 True
2 False
3 True
Name: color, dtype: bool
Quick Reference
| Usage | Description |
|---|---|
| Series.isin([values]) | Check if each element in Series is in the list of values |
| df[df['col'].isin([values])] | Filter DataFrame rows where 'col' values are in the list |
| isin accepts list, set, or any iterable | Input can be any iterable of values to check membership |
| Returns boolean Series or DataFrame mask | Use this mask to filter or select data |
Key Takeaways
Use
isin() to check if values in a column are in a list of values.Always pass a list or iterable to
isin(), not a single value.isin() returns a boolean mask useful for filtering DataFrames.It works on Series and DataFrame columns to test membership efficiently.
Combine
isin() with DataFrame filtering to select rows easily.