# Mean, median & grouped summaries

Code With Purpose · English · Free starter lesson

## Concept

The mean adds values and divides by their count. The median is the middle value after sorting, or the average of the two central values for an even count. Large extremes can move the mean much more than the median.

A grouped summary reveals differences that an overall average can hide. Include sample sizes: a mean from two observations has a different evidential weight from a mean from two thousand. Missing values are excluded by many Pandas aggregations, so report them when relevant.

Descriptive patterns do not establish cause and effect. A relationship between study time and marks could also reflect prior preparation or other factors. Here, use a deliberately tiny sample to practise calculations rather than make claims about learners. The code uses only Python’s standard library.

## Example

```python
from statistics import mean, median
hours = [1, 2, 2, 3, 12]
print(f"Mean: {mean(hours):.1f}")
print(f"Median: {median(hours):.1f}")
print(f"Count: {len(hours)}")
```

## Expected output

```text
Mean: 4.0
Median: 2.0
Count: 5
```

## Exercise

Calculate the mean and median after removing 12. Explain what changed, without assuming the extreme value is an error.

## Suggested solution

```
from statistics import mean, median
hours = [1, 2, 2, 3]
print(mean(hours))    # 2
print(median(hours))  # 2.0
# The mean falls; the median stays the same.
# Investigate whether 12 is valid before deciding to exclude it.
```

## Takeaway

Report context, sample size, and more than one summary.

Reference: https://docs.python.org/3/library/statistics.html

https://codewithpurposeai.in/lesson-data-summary.html?lang=english
