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])]

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.