Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas limit Series/DataFrame to range of values of one column

Tags:

pandas

I’d like to have a function df_out(df_in,val_min,val_max) that makes a sorted series/dataframe from another series/dataframe by picking rows where values in one column are within a defined range. E.g., if df_in looks like this:

Name   Age

John  13

Jack  19

Sylvia 21

Anna 14

Carlos 15

Vladimir 30

Gustav 28

Amie 24

I’d like df_out(18,25) to look like this:

Name Age

Jack 19

Sylvia 21

Amie 24

What's the most "pythonic" way to do this? Thanks!

like image 622
Alpha Avatar asked Nov 30 '25 02:11

Alpha


2 Answers

Why use a function when it is so easily done natively?

>>> df[df.Age.between(18, 25)]
     Name  Age
1    Jack   19
2  Sylvia   21
7    Amie   24

>>> df[df.Age.between(19, 24, inclusive=False)]
     Name  Age
2  Sylvia   21
like image 77
Alexander Avatar answered Dec 07 '25 20:12

Alexander


Once you have it in a DataFrame df, with columns Name, and Age, you can simply use

df[(min_val <= df.Age) & (df.Age <= max_val)]

Note that you need to use the seemingly-redundant parentheses in the above expression, due to operator precedence.


You can create this into a function like so:

def df_limited(df, min_val, max_val):
    return df[(min_val <= df.Age) & (df.Age <= max_val)]
like image 25
Ami Tavory Avatar answered Dec 07 '25 19:12

Ami Tavory



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!