In Python with pandas, weighted average for the sample data takes a few lines, shown below. It returns ₹597.50.
Weighted average = Σ (value × weight) ÷ Σ weights
Step by step
- Install the libraries (pip install pandas) and save daily.csv next to your script.
- Run the script and compare the printed value with the expected result.
The code
import pandas as pd
df = pd.read_csv("daily.csv")
weights = df["website_orders"] + df["app_orders"]
weighted_basket = (df["avg_basket"] * weights).sum() / weights.sum()
print(round(weighted_basket, 2))
Expected result on the sample data: ₹597.50.
What the result means
Here each day's average basket is weighted by that day's orders. Day 17 had the most orders and the smallest baskets (it was a sale), so the weighted average comes out below the simple average of the daily figures.
Notes for Python
- pandas methods such as .std(), .var() and .quantile() match Excel's sample versions (STDEV.S, VAR.S, PERCENTILE.INC). NumPy's np.std() and np.var() default to the population version instead.
- pandas skips NaN values in these calculations by default. Count how many rows were used if data might be missing.
Where weighted average misleads
- Pick the weight that matches the question. Average basket per order is weighted by orders; average price per unit is weighted by units.
- The weights must line up row for row with the values.
- If all weights are equal, the weighted and simple averages are the same, which is a quick check that the formula is right.
The plain-language explanation is on the Weighted average page.
The data
| day | website_orders | app_orders | avg_basket |
|---|---|---|---|
| 1 | 24 | 18 | 612 |
| 2 | 27 | 21 | 598 |
| 3 | 22 | 19 | 640 |
| 4 | 30 | 22 | 587 |
| 5 | 28 | 24 | 605 |
| 6 | 25 | 20 | 622 |
| 7 | 31 | 23 | 579 |
| 8 | 29 | 25 | 601 |
| 9 | 26 | 22 | 633 |
| 10 | 33 | 26 | 590 |
| 11 | 30 | 24 | 615 |
| 12 | 28 | 23 | 608 |
| 13 | 35 | 27 | 584 |
| 14 | 32 | 25 | 597 |
| 15 | 29 | 24 | 626 |
| 16 | 34 | 28 | 593 |
| 17 | 61 | 44 | 548 |
| 18 | 31 | 26 | 611 |
| 19 | 36 | 29 | 589 |
| 20 | 33 | 27 | 603 |
| 21 | 30 | 25 | 620 |
| 22 | 37 | 30 | 586 |
| 23 | 35 | 28 | 599 |
| 24 | 32 | 27 | 614 |
| 25 | 38 | 31 | 581 |
| 26 | 36 | 29 | 595 |
| 27 | 34 | 28 | 607 |
| 28 | 40 | 32 | 576 |
| 29 | 37 | 30 | 602 |
| 30 | 39 | 33 | 590 |
Same method, other tools
Related methods
Learn more about Python
- Python and SQL for data analysis, side by side — 3 min read
- Data analysis methods and techniques, and when to use each — 3 min read
About the data on this page
The figures come from a small made-up dataset, printed above in full so you can check every number by hand. Download it as daily.csv. The results are computed from those rows when the page is built, and the Excel formulas, SQL and Python on these pages are run against the same rows before publishing, so the code and the answers agree.
To get these numbers from your own data without writing formulas, try AnalyzeIt: ask in plain words and it shows the working behind every figure.
Legal entity
Devansh Yadav
Moradabad, Uttar Pradesh
Governing law: Courts at Moradabad, Uttar Pradesh
Phone: +91 8433475698
Legal: legal@analyzeit.in