Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Machine Learning by (19k points)

Let's say I have the following pandas dataframe:

>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 can subset based on a specific value:

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

x

  A B

2 3 3

But how can I subset based on a list of values? - something like this:

list_of_values = [3,6]

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

1 Answer

0 votes
by (33.1k points)

For this problem, you can use isin(). This function works the same as the SQL query. It searches for the given value in the dataframe to selects the entire row.

For example:

>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

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

   A B

1 6 2  

2 3 3

Hope this answer helps.

Learn Pandas Tutorial here.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...