Coder Perfect

To select rows from a Pandas dataframe, use a list of values.

Problem

Let’s say I have the Pandas dataframe below:

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

     A   B
0    5   1
1    6   2
2    3   3
3    4   5

I have the ability to subset depending on a specified value:

x = df[df['A'] == 3]
x

     A   B
2    3   3

How, on the other hand, can I subset based on a list of values? – anything along these lines:

list_of_values = [3,6]

y = df[df['A'] in list_of_values]

To get:

     A    B
1    6    2
2    3    3

Asked by zach

Solution #1

The isin technique can be used:

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

In [2]: df
Out[2]:
   A  B
0  5  1
1  6  2
2  3  3
3  4  5

In [3]: df[df['A'].isin([3, 6])]
Out[3]:
   A  B
1  6  2
2  3  3

And here’s how to use it the other way around:

In [4]: df[~df['A'].isin([3, 6])]
Out[4]:
   A  B
0  5  1
3  4  5

Answered by Wouter Overmeire

Solution #2

You can use the query method:

df.query('A in [6, 3]')
# df.query('A == [6, 3]')

or

lst = [6, 3]
df.query('A in @lst')
# df.query('A == @lst')

Answered by Mykola Zotko

Solution #3

Another method;

df.loc[df.apply(lambda x: x.A in [3,6], axis=1)]

This method, unlike the isin method, is especially useful for determining whether the list contains a function of column A. As an example, consider the function f(A) = 2*A – 5;

df.loc[df.apply(lambda x: 2*x.A-5 in [3,6], axis=1)]

This method is slower than the isin method, so keep that in mind.

Answered by Achintha Ihalage

Post is based on https://stackoverflow.com/questions/12096252/use-a-list-of-values-to-select-rows-from-a-pandas-dataframe