Skip to content
Justice Tech School

Totals by Row and Column

sum and mean take an axis: 0 works down columns and 1 across rows.

Example
df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
print(df.sum())
print(df.sum(axis=1).tolist())
print(df.mean().round(1).tolist())

Result

a    3
b    7
dtype: int64
[4, 6]
[1.5, 3.5]

Exercise

Row totals.

df.sum(=1)

Quiz Test

What does axis=0 sum?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.