# How to search DataFrames using pandas isin()

The [Python pandas](https://www.ionos.ca/digitalguide/websites/web-development/python-pandas/) function `DataFrame.isin()` is designed to quickly and efficiently check **whether certain values exist in a DataFrame**. This function is particularly useful for checking for multiple values at once.

## What is the syntax for pandas `isin()`?

Pandas `isin()` takes one parameter and looks like this:

```python
DataFrame.isin(values)
```

The `values` parameter can be a **[Python list](https://www.ionos.ca/digitalguide/websites/web-development/python-list/), a [Python dictionary](https://www.ionos.ca/digitalguide/websites/web-development/python-dictionary/) or another DataFrame**. It contains the values you want to search for in the DataFrame.

Tip If you’re working with pandas Series instead of DataFrames, you can use the equivalent function `Series.isin()`.

## How to use `isin()` with DataFrames in pandas

You can use `isin()` for different purposes. In addition to checking for values, it can also be used to filter DataFrames.

### Checking for values in a column

First, let’s take a look at a DataFrame that contains information about different people and where they live.

```python
import pandas as pd
# Creating a DataFrame
data = {
    'Name': ['Amir', 'Bella', 'Charlize', 'David'],
    'City': ['New York', 'Los Angeles', 'Chicago', 'Houston']
}
df = pd.DataFrame(data)
print(df)
```

The DataFrame looks like this:

```none
Name      City
0    Amir    New York
1    Bella  	Los Angeles
2  Charlize     Chicago
3    David    Houston
```

Now, we want to use pandas `isin()` to check whether the cities in the City column appear in a separate list of cities we’ve created. Once we’ve created the list with the reference cities, we’ll run the function on the DataFrame column “City”:

```python
# Cities for the list to be compared to
cities_to_check_against = ['Chicago', 'Houston', 'Miami']
# Using the isin() method
result = df['City'].isin(cities_to_check_against)
print(result)
```

The result is a series of Boolean values indicating whether each city in the City column is present in the `cities_to_check_against` list:

```none
0    False
1    False
2     True
3     True
Name: City, dtype: bool
```

### Filtering a DataFrame using `isin()`

You can also use pandas `isin()` to filter a DataFrame, keeping only the rows with cities that appear in the `cities_to_check_against` list.

```python
# Filtering a DataFrame using isin()
filtered_df = df[df['City'].isin(cities_to_check_against)]
print(filtered_df)
```

The result is a DataFrame that contains only the rows with cities that are also in the `cities_to_check_against` list:

```none
Name    City
2  Charlize  Chicago
3    David  Houston
```

### Checking multiple columns in a DataFrame

For more complex filtering operations, you can also use pandas `isin()` with dictionaries. In the following example, you’ll see how you can use a dictionary to simultaneously check multiple columns of a DataFrame. First, we’ll add a column to the original DataFrame and then use `isin()`:

```python
# Creating a DataFrame
data = {
    'Name': ['Amir', 'Bella', 'Charlize', 'David'],
    'City': ['New York', 'Los Angeles', 'Chicago', 'Houston'],
    'Age': [25, 30, 35, 40]
}
df = pd.DataFrame(data)
# Dictionary with values that the DataFrame should be checked against
values_to_check_against = {
    'City': ['Chicago', 'Houston'],
    'Age': [30, 40]
}
# Using isin() with a dictionary
result = df.isin(values_to_check_against)
print(result)
```

In this case, calling `isin()` returns a DataFrame with Boolean values, which indicate whether the conditions have been met in each column:

```none
Name  City  Age
0  False  False  False
1  False  False   True
2  False   True  False
3  False   True   True
```


This is a markdown version of: [https://www.ionos.ca/digitalguide/websites/web-development/python-pandas-dataframe-isin/](https://www.ionos.ca/digitalguide/websites/web-development/python-pandas-dataframe-isin/) for AI/LLM consumption.