如何计算 Pandas 的加权平均值?
加权平均是考虑数据集合中整数的相对值的计算。在计算加权平均值时,在完成最终计算之前,数据集中的每个值都按预定义的权重进行缩放。
句法:
def weighted_average(dataframe, value, weight):
val = dataframe[value]
wt = dataframe[weight]
return (val * wt).sum() / wt.sum()
它将返回项目价值的加权平均值。在分子中,我们将每个值与相关的相应权重相乘,然后将它们全部相加。在分母中,所有的权重都相加。
方法
- 我们采用数据框或制作自己的数据框。
- 定义一个函数,通过上述公式计算加权平均值。
- 我们需要在数据框中至少包含三个项目,即索引(可能是项目名称、日期或任何此类变量)、值和重量。
- 我们将进行一个函数调用,传递所有这三个值。
示例:
让我们看一个示例来计算按 item_name 分组的值的加权平均值。
假设有三个商店,每个商店包含三个项目,即巧克力、冰淇淋和饼干。我们有所有三个商店中每件商品的重量和每件商品的价格。现在我们需要找出每个项目的加权平均值。
Python3
import pandas as pd
def weighted_average(dataframe, value, weight):
val = dataframe[value]
wt = dataframe[weight]
return (val * wt).sum() / wt.sum()
# creating a dataframe to represent different
# items and their corresponding weight and value
dataframe = pd.DataFrame({'item_name': ['Chocolate', 'Chocolate',
'Chocolate', 'Biscuit',
'Biscuit', 'Biscuit',
'IceCream', 'IceCream',
'IceCream'],
'value': [90, 50, 86, 87, 42, 48,
68, 92, 102],
'weight': [4, 2, 3, 5, 6, 5, 3, 7,
5]})
# Weighted average of value grouped by item name
dataframe.groupby('item_name').apply(weighted_average,
'value', 'weight')
Python3
import pandas as pd
def weighted_average_of_group(values, weights, item):
return (values * weights).groupby(item).sum() / weights.groupby(item).sum()
# creating a dataframe to represent different items
# and their corresponding weight and value
dataframe = pd.DataFrame({'item_name': ['Chocolate', 'Chocolate', 'Chocolate',
'Biscuit', 'Biscuit', 'Biscuit',
'IceCream', 'IceCream', 'IceCream'],
'value': [90, 50, 86, 87, 42, 48, 68, 92, 102],
'weight': [4, 2, 3, 5, 6, 5, 3, 7, 5]})
# Finding grouped average of group
weighted_average_of_group(values=dataframe.value,
weights=dataframe.weight, item=dataframe.item_name)
Python3
import pandas as pd
def weighted_average(dataframe, value, weight):
val = dataframe[value]
wt = dataframe[weight]
return (val * wt).sum() / wt.sum()
# creating a dataframe to represent different items
# and their corresponding weight and value
dataframe = pd.DataFrame({'item_name': ['Chocolate', 'Chocolate', 'Chocolate',
'Biscuit', 'Biscuit', 'Biscuit',
'IceCream', 'IceCream', 'IceCream'],
'value': [90, 50, 86, 87, 42, 48, 68, 92, 102],
'weight': [4, 2, 3, 5, 6, 5, 3, 7, 5]})
# Weighted average of whole dataframe as a whole
weighted_average(dataframe, 'value', 'weight')
输出:
使用 groupby()
在这里,我们将使用 groupby()函数对项目进行分组,并通过将这些项目与 sum函数进行分组来计算权重。所以通过使用这种方法,我们只是形成一组相似的项目来获得总和
句法 :
def weighted_average_of_group(values, weights, item):
return (values * weights).groupby(item).sum() / weights.groupby(item).sum()
示例:
Python3
import pandas as pd
def weighted_average_of_group(values, weights, item):
return (values * weights).groupby(item).sum() / weights.groupby(item).sum()
# creating a dataframe to represent different items
# and their corresponding weight and value
dataframe = pd.DataFrame({'item_name': ['Chocolate', 'Chocolate', 'Chocolate',
'Biscuit', 'Biscuit', 'Biscuit',
'IceCream', 'IceCream', 'IceCream'],
'value': [90, 50, 86, 87, 42, 48, 68, 92, 102],
'weight': [4, 2, 3, 5, 6, 5, 3, 7, 5]})
# Finding grouped average of group
weighted_average_of_group(values=dataframe.value,
weights=dataframe.weight, item=dataframe.item_name)
输出:
要计算整个数据帧(不是每个组,而是作为一个整体)的加权平均值,我们将使用如下所示的语法:
句法
def weighted_average_of_whole_dataframe(dataframe, value, weight):
val = dataframe[value]
wt = dataframe[weight]
return (val * wt).sum() / wt.sum()
示例:
Python3
import pandas as pd
def weighted_average(dataframe, value, weight):
val = dataframe[value]
wt = dataframe[weight]
return (val * wt).sum() / wt.sum()
# creating a dataframe to represent different items
# and their corresponding weight and value
dataframe = pd.DataFrame({'item_name': ['Chocolate', 'Chocolate', 'Chocolate',
'Biscuit', 'Biscuit', 'Biscuit',
'IceCream', 'IceCream', 'IceCream'],
'value': [90, 50, 86, 87, 42, 48, 68, 92, 102],
'weight': [4, 2, 3, 5, 6, 5, 3, 7, 5]})
# Weighted average of whole dataframe as a whole
weighted_average(dataframe, 'value', 'weight')
输出:
75.075