Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Applying multiple greater than and less than grouping rows by specific column

I am creating 3 pandas dataframes based off of one original pandas dataframe. I have calculated standard deviations from the norm.

#Mean
stats_over_29000_mean = stats_over_29000['count'].mean().astype(int)

152542

#STDS
stats_over_29000_count_between_std = stats_over_29000_std - stats_over_29000_mean

54313

stats_over_29000_first_std = stats_over_29000_mean + stats_over_29000_count_between_std

206855

stats_over_29000_second_std = stats_over_29000_first_std + stats_over_29000_count_between_std

261168

stats_over_29000_third_std = stats_over_29000_second_std + stats_over_29000_count_between_std

315481

This works to get all rows from df under 2 stds

#Select all rows where count is less than 2 standard deviations 
stats_under_2_stds = stats_over_29000[stats_over_29000['count'] < stats_over_29000_second_std]

Next I would like to select all rows from df where >=2 stds and less than 3 stds

I have tried:

stats_2_and_over_under_3_stds = stats_over_29000[stats_over_29000['count'] >= stats_over_29000_second_std < stats_over_29000_third_std]

and

stats_2_and_over_under_3_stds = stats_over_29000[stats_over_29000['count'] >= stats_over_29000_second_std && < stats_over_29000_third_std]

But neither seem to work.

like image 662
sectechguy Avatar asked Oct 20 '25 07:10

sectechguy


1 Answers

This is how you filter on df with 2 conditions :

  • init df = pd.DataFrame([[1,2],[1,3],[1,5],[1,8]],columns=['A','B'])
  • operation : res = df[(df['B']<8) & (df['B']>2)]
  • result :

       A  B
    1  1  3
    2  1  5
    

In your case :

stats_2_and_over_under_3_stds = stats_over_29000[(stats_over_29000['count'] >= stats_over_29000_second_std) & (stats_over_29000['count'] < stats_over_29000_third_std)]
like image 76
Etienne Herlaut Avatar answered Oct 22 '25 21:10

Etienne Herlaut



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!