Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter a column by multiple values

I'm new at Python and I want to filter rows by multiple column values. The name of my dataframe is df, the column name is values and I want to filter this column by the following values:

2, 4, 5, 9

My dataframe is like this:

name value
Jon    4
Ron    5
Paul   10
Max    3 

So grateful, Henrique.

like image 765
Henrique Girotto BR Avatar asked Apr 02 '26 15:04

Henrique Girotto BR


1 Answers

There are two ways to do this:

df[(df["value"]==2) | (df["value"]==4) | (df["value"]==5) | (df["value"]==9)]

OR

numbers = [2, 4, 5, 9]
df[df["value"].isin(numbers)]
like image 143
betelgeuse Avatar answered Apr 04 '26 04:04

betelgeuse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!