Stacking and Splitting

Python · NumPy Library

Theory

Stacking joins arrays into one. np.concatenate([a, b], axis=0) joins along an existing axis; axis=0 adds rows and axis=1 adds columns. np.vstack puts arrays on top of each other (more rows) and np.hstack places them side by side (more columns). np.column_stack turns several 1-D arrays into the columns of a table, which is a quick way to build a feature matrix.

The sizes must agree on every axis except the one you join along: to vstack, both arrays need the same number of columns. Splitting is the reverse: np.split(arr, 3) cuts into 3 equal pieces and fails if the size does not divide evenly, while np.array_split allows unequal pieces. np.split(arr, [2, 5]) cuts at positions 2 and 5. All of these return new arrays.

Why we use it

Data often arrives in pieces, such as monthly files, separate feature arrays or batches. Stacking combines them into one array for analysis, and splitting cuts a big array into parts for batches, train and test sets or per-group work.

When to use it

Use vstack or concatenate(axis=0) to add rows, hstack or column_stack to add columns, split for equal chunks and array_split when sizes may not divide evenly. For tables with labels, pandas concat is the usual choice.

How it works

  1. Several arrays to combine
  2. Adding rows? Yes: vstack or concatenate(axis=0). No: hstack or concatenate(axis=1).
  3. Do the other dimensions match? Yes: Arrays are joined into one. No: ValueError: dimensions must match.
  4. Later, cut it again with np.split or np.array_split
  5. One combined array, or a list of pieces

Syntax

np.concatenate([a, b], axis=0)
np.vstack([a, b])  np.hstack([a, b])
np.column_stack([x1, x2])
np.split(a, n)  np.split(a, [i, j])  np.array_split(a, n)

Example

jan = np.array([[120, 135]])
feb = np.array([[90, 95]])
print(np.vstack([jan, feb]))

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