Aggregations and Axis

Python · NumPy Library

Theory

Aggregations reduce many values to one: sum, mean, min, max, std, plus argmin and argmax, which return the position of the smallest or largest value. Called with no axis they summarise the whole array. On a 2-D array the axis argument decides the direction: axis=0 collapses the rows and gives one result per column, while axis=1 collapses the columns and gives one result per row.

For a table of 3 stores (rows) by 3 months (columns), sales.sum(axis=1) is each store's total and sales.sum(axis=0) is each month's total across stores. sales.argmax(axis=1) tells you which month was best for each store. np.cumsum gives running totals, keepdims=True keeps the reduced axis so the result can be broadcast back, and the nan-versions (np.nansum, np.nanmean) skip missing values.

Why we use it

Totals, averages and best or worst positions are the core of every report. The axis argument lets you get per-row or per-column summaries of a whole table in one call instead of writing nested loops.

When to use it

Use axis=0 for per-column (per-feature) statistics and axis=1 for per-row (per-record) ones. Use argmax when you need where the maximum is, not its value. If the data has labels or groups, pandas groupby is often clearer.

How it works

  1. 2-D array (rows x columns)
  2. Need one number for the whole array? Yes: Call a.sum() with no axis. No: Choose an axis.
  3. Summary per column? Yes: axis=0 collapses rows. No: axis=1 collapses columns (per row).
  4. Need the position of the max? use argmax instead of max
  5. 1-D result with one value per column or row

Syntax

a.sum()  a.mean()  a.min()  a.max()  a.std()
a.sum(axis=0)   # per column
a.sum(axis=1)   # per row
a.argmax(axis=1)  np.cumsum(a)

Example

sales = np.array([[120, 135], [90, 95]])
print(sales.sum(axis=0))   # per month
print(sales.sum(axis=1))   # per store

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic