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!
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
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)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With