如何获得加权平均价格的每一个项目从一组多个项目,到一个新的dataframe使用PythonPandas?

iszxjhcz  于 2022-12-28  发布在  Python
关注(0)|答案(1)|浏览(134)

我有一个包含多个项目的数据框,每个项目在一年内以不同的价格和数量被多次购买,我需要找到每个项目的加权平均值,并将每个项目及其加权平均价格Map到一个新的数据框中。

Year Article  Price  Weight
18  2013  Cheese   18.0    26.0
19  2013   Apple    5.0     7.0
20  2013     Bun    2.0     2.0
21  2013   Coatl    4.0     5.0
22  2013  Cheese   20.0    21.0
23  2013   Peach   12.0     8.0
24  2013   Apple    4.6     3.0
import pandas as pd

df = newonly2013

def weightedAverage(df,Weight,Price):
    return sum(df['Weight']*df['Price'])/sum(df['Weight'])

wa=weightedAverage(df,'Weight','Price')

ar = df.groupby('Article', as_index=False).apply(weightedAverage)
print(ar)

这是我尝试和失败,将不胜感激,如果有人能帮助我在这里。谢谢!

mpbci0fu

mpbci0fu1#

使用自定义函数时,仅将df作为参数传递:

def weightedAverage(df):
    return sum(df['Weight']*df['Price'])/sum(df['Weight'])

ar = df.groupby('Article', as_index=False).apply(weightedAverage)

或者,如果需要能够传递列名:

def weightedAverage(df, Weight, Price):
    return sum(df[Weight]*df[Price])/sum(df['Weight'])

ar = (df.groupby('Article', as_index=False)
        .apply(weightedAverage, Weight='Weight', Price='Price')
      )

或者,使用numpy.average及其weights参数:

import numpy as np

out = (df.groupby('Article')
         .apply(lambda d: np.average(d['Price'], weights=d['Weight']))
       )

输出:

Article
Apple      4.880000
Bun        2.000000
Cheese    18.893617
Coatl      4.000000
Peach     12.000000
dtype: float64

作为数据框:

out = (df.groupby('Article')
         .apply(lambda d: np.average(d['Price'], weights=d['Weight']))
         .rename('average').reset_index()
      )

输出:

Article    average
0   Apple   4.880000
1     Bun   2.000000
2  Cheese  18.893617
3   Coatl   4.000000
4   Peach  12.000000

相关问题