我有这个DataFrame:

python - 如何对系列对象中每个类别的值求和?-LMLPHP

我需要做的是按Category列对DataFrame进行分组,然后使用一个函数按每个类别计算总权重。到目前为止,这是我所做的...

def multiply(group, quantity, weight):
  x = group[quantity]
  y = group[weight]

  return x * y

print(df.groupby('Category').apply(multiply, 'Quantity',  'Weight (oz.)'))


我得到的结果是

python - 如何对系列对象中每个类别的值求和?-LMLPHP

现在,如何计算得出的Series对象的每个类别(例如,包装,庇护所,睡眠等)的总重量?

最佳答案

使用数据样本:

Item,Category,Quantity,Weight (oz.)
Sleeping Pad,Sleep,1,80.0
Sleeping Bag,Sleep,1,20.0
Spoon,Kitchen,1,0.87
Stove,Kitchen,1,20.0
Water Filter,Kitchen,1,1.8
Water Bottles,Kitchen,2,35.0

In [1]: df = pd.read_clipboard(sep=',', index_col=0)


In [2]: df.groupby('Category').apply(lambda x: (x['Quantity'] *x['Weight (oz.)']).sum())
Out[2]:
Category
Kitchen     92.67
Sleep      100.00
dtype: float64

10-08 04:36