In Python with pandas, geometric mean for the sample data takes a few lines, shown below. It returns ₹600.08.
Geometric mean = (x₁ × x₂ × … × xₙ)^(1/n) = exp(average of ln x)
Step by step
- Install the libraries (pip install pandas numpy) and save daily.csv next to your script.
- Run the script and compare the printed value with the expected result.
The code
import numpy as np
import pandas as pd
df = pd.read_csv("daily.csv")
geometric_mean = np.exp(np.log(df["avg_basket"]).mean())
print(round(geometric_mean, 2))
Expected result on the sample data: ₹600.08.
What the result means
Applied to the daily baskets it is slightly below the arithmetic mean, as it always is unless every value is the same. The gap grows with the spread of the values. For growth, take the geometric mean of the growth factors: monthly factors of 1.10 and 0.90 have a geometric mean just under 1, which correctly says the business shrank slightly.
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 geometric mean misleads
- It only works for positive numbers. For growth rates, average the growth factors (1.10, 0.90) and subtract 1 at the end.
- Computing the product directly can overflow; the logarithm form avoids that.
- It is not a replacement for the mean when you need a total.
The plain-language explanation is on the Geometric mean page.
The data
| day | avg_basket |
|---|---|
| 1 | 612 |
| 2 | 598 |
| 3 | 640 |
| 4 | 587 |
| 5 | 605 |
| 6 | 622 |
| 7 | 579 |
| 8 | 601 |
| 9 | 633 |
| 10 | 590 |
| 11 | 615 |
| 12 | 608 |
| 13 | 584 |
| 14 | 597 |
| 15 | 626 |
| 16 | 593 |
| 17 | 548 |
| 18 | 611 |
| 19 | 589 |
| 20 | 603 |
| 21 | 620 |
| 22 | 586 |
| 23 | 599 |
| 24 | 614 |
| 25 | 581 |
| 26 | 595 |
| 27 | 607 |
| 28 | 576 |
| 29 | 602 |
| 30 | 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