Pandas – Select Rows where column value is in List

This tutorial will discuss about different ways to select DataFrame rows where column value is in list in Pandas.

Table Of Contents

Preparing DataSet

Let’s create a DataFrame with some dummy data.

import pandas as pd

data = {'Col_A': [33, 12, 33, 14, 35, 36, 17],
        'Col_B': [21, 22, 23, 24, 25, 26, 27],
        'Col_C': [33, 32, 33, 35, 35, 36, 35]}

index = ['X1', 'X2', 'X3', 'X4', 'X5', 'X6', 'X7']

# Create a DataFrame from a dictionary
df = pd.DataFrame.from_dict(data)

# Set the index list as Index of DataFrame
df.set_index(pd.Index(index), inplace=True)

print (df)

Output

    Col_A  Col_B  Col_C
X1     33     21     33
X2     12     22     32
X3     33     23     33
X4     14     24     35
X5     35     25     35
X6     36     26     36
X7     17     27     35

Now we will operate on this DataFrame.

Select DataFrame Rows where a column has any value from list

Suppose we have a list of values and we want to select only those rows from a DataFrame where a specific column contains any value from the given list.

For that, we are going to select that particular column as a Pandas Series object, and call isin() function on that column. Pass the given list of values as an argument in the isin() method. It will return as a boolean array, where each True value represent that the particular column value exists in the given list.

Then pass this boolean series into the loc[] attribute of DataFrame, and it will return a subset of DataFrame containing only those rows, value in the specified column also exists in the list.

For example, we are going to select rows where values in the column Col_B, is in the given list.

listOfValues = [22, 26, 25]

# Select rows where any value from the column
# "COL_B" is in the list
subDf = df.loc[df['Col_B'].isin(listOfValues) ]

print(subDf)

Output

    Col_A  Col_B  Col_C
X2     12     22     32
X5     35     25     35
X6     36     26     36

Summary

We learned how to select DataFrame rows where values in a specific column is in a List. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top