【问题标题】:How to exclude rows from a groupby operation如何从 groupby 操作中排除行
【发布时间】:2022-10-06 20:21:09
【问题描述】:

我正在使用 attribute 列进行 groupby 操作,但我想排除将用于计算每个属性内总折扣的 desc_type 1 and 2

pd.DataFrame({\'ID\':[10,10,10,20,30,30],\'attribute\':[\'attrib_1\',\'desc_type1\',\'desc_type2\',\'attrib_1\',\'attrib_2\',\'desc_type1\'],\'value\':[100,0,0,100,30,0],\'discount\':[0,6,2,0,0,13.3]})

输出:

ID       attribute       value      discount
10       attrib_1        100          0
10       desc_type1       0           6
10       desc_type2       0           2
20       attrib_1         100         0
30       attrib_2         30          0
30       desc_type1       0           13.3

我想通过attribute 对这个数据框进行分组,但不包括desc_type1 and desc_type2

所需的输出:

attribute     ID_count    value_sum   discount_sum
attrib_1         2          200          8
attrib_2         1          30          13.3

解释:

attrib_1折扣总和=8因为编号 30属于attrib_1的有两个desc_type

attrib_2折扣总和=13.3因为编号 10有一个 desc_type

ID=20 没有折扣类型。

到目前为止我做了什么:

df.groupby(\'attribute\').agg({\'ID\':\'count\',\'value\':\'sum\',\'discount\':\'sum\'})

但是上面的行并没有从 groupby 中排除 desc_type 1 and 2

重要提示:ID 可能有折扣或没有折扣。

    标签: python-3.x pandas jupyter-notebook


    【解决方案1】:

    您好,我认为这有帮助:

    df.loc[(df['attribute'] != 'desc_type1') &( df['attribute'] != 'desc_type2')].groupby('attribute').agg({'ID':'count','value':'sum','discount':'sum'})
    
    

    输出 :

        ID  value   discount
    attribute           
    attrib_1    2   200 0.0
    attrib_2    1   30  0.0
    

    【讨论】:

      猜你喜欢
      • 2018-10-21
      • 1970-01-01
      • 2015-10-15
      • 2019-12-21
      • 1970-01-01
      • 2017-01-02
      • 1970-01-01
      • 2016-09-04
      • 2022-12-11
      相关资源
      最近更新 更多