Series Basics
Series Aggregate
Reduce a list of values to a single sum and mean with a manual accumulator
loop. The pandas version calls .sum() and .mean() directly on a Series,
showing how aggregations collapse the entire index-and-values structure to a
scalar.
By hand
Accumulate total by adding each value in a loop. Compute mean as total
divided by the count, rounded to two decimal places.
naive.py
Replay: real traced execution (multi-file project)
values = [4, 7, 2, 9, 3]
total = 0
for v in values:
total += v
mean = round(total / len(values), 2)
print('RESULT:', (total, mean))
values ← [4, 7, 2, 9, 3]
1values = [4, 7, 2, 9, 3]2total = 0values this step[4, 7, 2, 9, 3]valuestotal ← 0
1values = [4, 7, 2, 9, 3]2total = 03for v in values:values this step0totalv ← 4
2total = 03for v in values:4 total += vvalues this step4vtotal ← 4
3for v in values:4 total += v5mean = round(total / len(values), 2)values this step0 → 4totalv ← 7
2total = 03for v in values:4 total += vvalues this step4 → 7vtotal ← 11
3for v in values:4 total += v5mean = round(total / len(values), 2)values this step4 → 11totalv ← 2
2total = 03for v in values:4 total += vvalues this step7 → 2vtotal ← 13
3for v in values:4 total += v5mean = round(total / len(values), 2)values this step11 → 13totalv ← 9
2total = 03for v in values:4 total += vvalues this step2 → 9vtotal ← 22
3for v in values:4 total += v5mean = round(total / len(values), 2)values this step13 → 22totalv ← 3
2total = 03for v in values:4 total += vvalues this step9 → 3vtotal ← 25
3for v in values:4 total += v5mean = round(total / len(values), 2)values this step22 → 25totalfor v in values:
2total = 03for v in values:4 total += vmean ← 5.0
4 total += v5mean = round(total / len(values), 2)6print('RESULT:', (total, mean))values this step5.0meanstdout ← RESULT: (25, 5.0)
5mean = round(total / len(values), 2)6print('RESULT:', (total, mean))values this stepRESULT: (25, 5.0)stdout
With pandas
s.sum() and s.mean() reduce the Series to scalars. int() and float()
convert the numpy scalars to plain Python types for a stable RESULT.
library.py
import pandas as pd
from dalib.display import set_display
set_display()
values = [4, 7, 2, 9, 3]
s = pd.Series(values)
total = int(s.sum())
mean = round(float(s.mean()), 2)
print('index:', s.index.tolist())
print('values:', s.tolist())
print('dtype:', s.dtype)
print('sum:', total)
print('mean:', mean)
print('RESULT:', (total, mean))
index: [0, 1, 2, 3, 4]
values: [4, 7, 2, 9, 3]
dtype: int64
sum: 25
mean: 5.0
RESULT: (25, 5.0)
Implementation notes
s.sum()ands.mean()return numpy scalars (np.int64,np.float64). Wrapping withint()/float()converts them to Python natives, avoiding repr differences across numpy versions (see the NumPysum-and-meanlesson).- Aggregation methods ignore the index and operate only on the values array.
Any index — default
RangeIndexor string labels — produces the same sum and mean. - Other common aggregations on a Series:
.min(),.max(),.std(),.median(),.count(). All reduce to a scalar and share the same numpy-scalar return type pattern. - Cross-reference:
sum-and-mean(python-numpy ch05) for the NumPy array version;list-sum-mean(python-data-basics) for the plain Python version.