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 call the isin method.

For instanmce, we write

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

to create the df data frame and get the values from column 'A' that’s in rows 3 to 6 with isin.

We can also get the rows that aren’t in 3 to 6 with

df[~df['A'].isin([3, 6])]

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 *