如何在Python中计算所有其他产品的加权平均值

huangapple go评论89阅读模式
英文:

How to calculate weighted average of all other products in python

问题

我需要创建一个新列,该列是所有子类别(在一个类别内)的加权平均价格(根据类别内的收入进行加权),除了子类别列中的那一个,即对于第一行,我只需要A2和A3的加权平均价格,因为子类别列中的值是A1。可以有人帮忙吗?

英文:

I have a dataframe as below

  1. category sub category price revenue
  2. A A1 100 1000
  3. A A2 110 990
  4. A A3 120 890
  5. B B1 90 1200
  6. B B2 100 1100
  7. B B3 95 1050

I need to create a new column which is the weighted avg price(weighted with revenue within a category) for all subcategories(within a category) except the one in subcategory column,i.e for the 1st row, I need the weighted avg price of A2 & A3 only since A1 is the value in sub-category column. Can someone pls help?

答案1

得分: 1

你可以手动计算加权均值,同时减去自身的值:

  1. tmp = (df.set_index(['category', 'sub category'])
  2. .eval('prod=price*revenue')
  3. )
  4. g = tmp.groupby(level=0)
  5. out = (g['prod'].transform('sum')
  6. .sub(tmp['prod'])
  7. .div(g['revenue'].transform('sum').sub(tmp['revenue']))
  8. )

输出:

  1. category sub category
  2. A A1 114.734043
  3. A2 109.417989
  4. A3 104.974874
  5. B B1 97.558140
  6. B2 92.333333
  7. B3 94.782609
  8. dtype: float64
英文:

You can compute the weighted mean manually, while subtracting the self values:

  1. tmp = (df.set_index(['category', 'sub category'])
  2. .eval('prod=price*revenue')
  3. )
  4. g = tmp.groupby(level=0)
  5. out = (g['prod'].transform('sum')
  6. .sub(tmp['prod'])
  7. .div(g['revenue'].transform('sum').sub(tmp['revenue']))
  8. )

Output:

  1. category sub category
  2. A A1 114.734043
  3. A2 109.417989
  4. A3 104.974874
  5. B B1 97.558140
  6. B2 92.333333
  7. B3 94.782609
  8. dtype: float64

huangapple
  • 本文由 发表于 2023年7月13日 13:36:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676223.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定