熊猫应用多个大于和小于按特定列对行进行分组

sec*_*guy 1 python python-3.x pandas

我正在基于一个原始熊猫数据框创建 3 个熊猫数据框。我已经计算了与规范的标准偏差。

#Mean
stats_over_29000_mean = stats_over_29000['count'].mean().astype(int)
Run Code Online (Sandbox Code Playgroud)

152542

#STDS
stats_over_29000_count_between_std = stats_over_29000_std - stats_over_29000_mean
Run Code Online (Sandbox Code Playgroud)

54313

stats_over_29000_first_std = stats_over_29000_mean + stats_over_29000_count_between_std
Run Code Online (Sandbox Code Playgroud)

206855

stats_over_29000_second_std = stats_over_29000_first_std + stats_over_29000_count_between_std
Run Code Online (Sandbox Code Playgroud)

261168

stats_over_29000_third_std = stats_over_29000_second_std + stats_over_29000_count_between_std
Run Code Online (Sandbox Code Playgroud)

315481

这适用于从 df 获取 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]
Run Code Online (Sandbox Code Playgroud)

接下来我想从 df 中选择所有行,其中 >=2 stds 且少于 3 stds

我试过了:

stats_2_and_over_under_3_stds = stats_over_29000[stats_over_29000['count'] >= stats_over_29000_second_std < stats_over_29000_third_std]
Run Code Online (Sandbox Code Playgroud)

stats_2_and_over_under_3_stds = stats_over_29000[stats_over_29000['count'] >= stats_over_29000_second_std && < stats_over_29000_third_std]
Run Code Online (Sandbox Code Playgroud)

但两者似乎都不起作用。

Eti*_*aut 8

这是使用 2 个条件过滤 df 的方法:

在你的情况下:

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)]
Run Code Online (Sandbox Code Playgroud)


Syl*_*ain 5

Pandas 现在有Series.between(left, right, inclusive=True), 允许同时进行两种比较

在你的情况下:

stats_2_and_over_under_3_stds = \
  stats_over_29000[(stats_over_29000['count'].between(
  stats_over_29000_second_std, stats_over_29000_third_std)]
Run Code Online (Sandbox Code Playgroud)