Categories
Python Answers

How to use a list of values to select rows from a Python Pandas dataframe?

Spread the love

To use a list of values to select rows from a Python Pandas dataframe, we use the isin method.

For instance, we write

df = pd.DataFrame({'A': [5,6,3,4], 'B': [1,2,3,5]})
df[df['A'].isin([3, 6])]

to create the df data frame.

Then we call isin with df['A'] to select the items from column A that’s in [3, 6].

So the rows with 3 or 6 are returned from column A.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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