Weighted Average Calculation in Pandas
A weighted average in pandas is the sum of value times weight divided by the sum of the weights: (df["Score"] * df["Weight"]).sum() / df["Weight"].sum(). np.average does the same through its weights argument, and a small helper with groupby returns one weighted average per group.

The example
A weighted average gives some values more influence than others. In the example, a teacher grades a quiz, a test and a final exam with the weights 1, 2 and 3, so the final exam counts three times as much as the quiz.
import pandas as pd
df = pd.DataFrame(
{
"Score": [20, 40, 90],
"Weight": [1, 2, 3],
"Assessment": ["Quiz", "Test", "Final Exam"],
}
)
df
Score Weight Assessment
0 20 1 Quiz
1 40 2 Test
2 90 3 Final Exam
The calculation has two steps:
- First, multiply each score by its respective weight. For example:
- Quiz: 20 * 1
- Test: 40 * 2
- Final Exam: 90 * 3
- Then, divide the total of these products by the sum of the weights.
The formula in pandas
In pandas the formula is one line: multiply the Score column by the Weight column, sum the products and divide by the sum of the weights. The comparison with the simple mean shows how much the weights change the result:
import pandas as pd
df = pd.DataFrame(
{
"Score": [20, 40, 90],
"Weight": [1, 2, 3],
"Assessment": ["Quiz", "Test", "Final Exam"],
}
)
# Simple mean: (20 + 40 + 90) / 3 = 50
average = df["Score"].mean()
# Weighted: (20*1 + 40*2 + 90*3) / (1 + 2 + 3) = 370 / 6 = 61.67
weighted_avg = (df["Score"] * df["Weight"]).sum() / df["Weight"].sum()
The simple mean is 50, the weighted average 61.67, because the high exam score carries the most weight.
One group only
For one group, filter the rows first and apply the same formula. The second DataFrame adds a Student column; df.query("Student == 'B'") keeps Student B’s three rows.
df = pd.DataFrame(
{
"Score": [20, 40, 90, 80, 60, 10],
"Weight": [1, 2, 3, 1, 2, 3],
"Student": ["A", "A", "A", "B", "B", "B"],
"Assessment": ["Quiz", "Test", "Final Exam", "Quiz", "Test", "Final Exam"],
}
)
The filtered DataFrame and its weighted average look like this:
df_filtered = df.query("Student == 'B'")
df_filtered
weighted_avg = (df_filtered["Score"] * df_filtered["Weight"]).sum() / df_filtered["Weight"].sum()
weighted_avg
Score Weight Student Assessment
3 80 1 B Quiz
4 60 2 B Test
5 10 3 B Final Exam
38.333333333333336
Filtering works for one student, but repeating it for every student gets tedious.
Every group with groupby
For every group at once, a two-line helper groups both the products and the weights by the same column and divides the sums. It takes the values, the weights and the grouping column, and works on the full data in one vectorized step.
df = pd.DataFrame(
{
"Score": [20, 40, 90, 80, 60, 10, 5, 60, 90],
"Weight": [1, 2, 3, 1, 2, 3, 1, 2, 3],
"Student": ["A", "A", "A", "B", "B", "B", "C", "C", "C"],
"Assessment": ["Quiz", "Test", "Final Exam", "Quiz", "Test", "Final Exam", "Quiz", "Test", "Final Exam"],
}
)
The helper:
def grouped_weighted_avg(values, weights, by):
return (values * weights).groupby(by).sum() / weights.groupby(by).sum()
grouped_weighted_avg(df["Score"], df["Weight"], df["Student"])
With three students and the scores from the video, the result is one value per student:
Student
A 61.666667
B 38.333333
C 65.833333
dtype: float64
The function returns a pandas Series indexed by student, so you can use it like any other result, for example with .to_frame("Weighted Average") to turn it into a DataFrame.
np.average
NumPy’s np.average takes a weights argument and returns the same result as the formula: 61.67 for the first example and 38.33 for Student B. Combined with groupby().apply() it gives one value per group as well:
import numpy as np
# One group
np.average(df_filtered["Score"], weights=df_filtered["Weight"])
# Every group
df.groupby("Student").apply(
lambda g: np.average(g["Score"], weights=g["Weight"]), include_groups=False
)
The include_groups=False argument needs pandas 2.2 or later and silences a deprecation warning; leave it out on older versions. I prefer the vectorized helper above for large data, because apply calls Python once per group. On the example data, the helper and the np.average version return identical numbers.
Pitfalls
Most wrong weighted averages come from three causes: missing values, the wrong denominator, or weights that add up to zero.
- Missing scores. A NaN score drops out of the product sum, but its weight still counts in the denominator, so the result comes out too low. With scores 20, NaN and 90 the formula returns 48.33 instead of 72.5. Drop or fill those rows first; this groupby mistake shows how missing values change totals.
- Dividing by the row count. A weighted average divides by the sum of the weights, not by the number of rows. With weights 1, 2 and 3 that is 6, not 3.
- Weights that sum to zero. Then the result is undefined: the pandas formula divides by zero and returns inf (or NaN when the products also sum to zero), and np.average raises a ZeroDivisionError.
If you come from Excel, this is the pandas version of SUMPRODUCT divided by SUM; the other conditional formulas are in SUMIFS and AVERAGEIFS in pandas. To practice on a realistic sales table, my free Excel dataset generator creates one with quantities you can use as weights.
