0
0
NumPydata~5 mins

Aggregation along specific axes in NumPy

Choose your learning style9 modes available
Introduction

Aggregation helps us summarize data by combining values. Doing it along specific axes lets us focus on rows or columns separately.

You want to find the average score of each student across subjects.
You need the total sales per product from a sales table.
You want to find the maximum temperature each day from hourly data.
You want to count how many times each category appears in columns.
Syntax
NumPy
numpy_function(array, axis=axis_number)

array is your data in a numpy array.

axis=0 means aggregate down the rows (column-wise).

axis=1 means aggregate across the columns (row-wise).

Examples
This sums each column: result is [4, 6].
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
np.sum(arr, axis=0)
This sums each row: result is [3, 7].
NumPy
np.sum(arr, axis=1)
This finds the average of each column: [2.0, 3.0].
NumPy
np.mean(arr, axis=0)
This finds the max value in each row: [2, 4].
NumPy
np.max(arr, axis=1)
Sample Program

This program shows how to sum scores for each test across all students and find each student's average score.

NumPy
import numpy as np

# Create a 3x4 array representing 3 students and 4 test scores
scores = np.array([
    [80, 90, 70, 85],
    [75, 85, 80, 90],
    [90, 95, 85, 100]
])

# Sum scores for each test (column-wise sum)
total_per_test = np.sum(scores, axis=0)

# Average score for each student (row-wise mean)
avg_per_student = np.mean(scores, axis=1)

print("Total scores per test:", total_per_test)
print("Average score per student:", avg_per_student)
OutputSuccess
Important Notes

Remember axis=0 collapses rows and keeps columns.

Axis numbering starts at 0 for the first dimension.

Aggregation functions include sum, mean, max, min, etc.

Summary

Aggregation combines data to summarize it.

Use axis=0 to aggregate down rows (per column).

Use axis=1 to aggregate across columns (per row).