Question: how to calculate weighted average in pandas?
Answer: import pandas as pd.
import numpy as np.
df = pd.DataFrame({'a': [300, 200, 100], 'b': [10, 20, 30]})
# using formula.
wm_formula = (df['a']*df['b']).sum()/df['b'].sum()
# using numpy average() method.
wm_numpy = np.average(df['a'], weights=df['b'])