我正在基于一个原始熊猫数据框创建 3 个熊猫数据框。我计算了与标准的标准偏差。
#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
这适用于从 2 个标准下的 df 获取所有行
#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]
接下来我想从 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]
和
stats_2_and_over_under_3_stds = stats_over_29000[stats_over_29000['count'] >= stats_over_29000_second_std && < stats_over_29000_third_std]
但似乎都不起作用。
原文由 sectechguy 发布,翻译遵循 CC BY-SA 4.0 许可协议
这是使用 2 个条件过滤 df 的方式:
df = pd.DataFrame([[1,2],[1,3],[1,5],[1,8]],columns=['A','B'])
res = df[(df['B']<8) & (df['B']>2)]
在你的情况下: