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')