【发布时间】: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