panda应用多个大于和小于按特定列分组的行

2024-03-28 18:13:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建3熊猫数据帧的基础上一个原始熊猫数据帧。我已经计算出标准偏差。在

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

152542个

^{pr2}$

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号

这样可以从df中获取2个std下的所有行

#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 std且小于3 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]

以及

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

但两者似乎都不管用。在


Tags: and数据dfstatscountbetweenmean基础
1条回答
网友
1楼 · 发布于 2024-03-28 18:13:04

这是您在两个条件下过滤df的方法:

  • 初始化df = pd.DataFrame([[1,2],[1,3],[1,5],[1,8]],columns=['A','B'])
  • 操作:res = df[(df['B']<8) & (df['B']>2)]
  • 结果:

       A  B
    1  1  3
    2  1  5
    

就您而言:

^{pr2}$

相关问题 更多 >