Array Shape and Reshaping

Python · NumPy Library

Theory

The shape of an array is a tuple giving its size along each axis: (6,) is a 1-D array of six values, (2, 3) is two rows and three columns. reshape rearranges the same values into a new shape without changing their order, so np.arange(1, 7).reshape(2, 3) fills row by row: [[1, 2, 3], [4, 5, 6]]. The total size must stay the same, and one dimension can be -1 to let NumPy work it out: reshape(3, -1) gives (3, 2).

To go back to 1-D, ravel() returns a flat view when it can, while flatten() always returns an independent copy. arr.T (transpose) swaps rows and columns, turning (2, 3) into (3, 2). arr.reshape(-1, 1) or arr[:, np.newaxis] turns a 1-D array into a single column, the shape scikit-learn expects for one feature.

Why we use it

Data rarely arrives in the shape a function needs. Reshaping lets you turn a flat list of readings into a table, a single feature into a column for scikit-learn, or a matrix into rows ready for plotting, without copying or retyping data.

When to use it

Use reshape when the total number of values fits the new shape, -1 when one size is obvious, .T to swap rows and columns, and ravel or flatten to go back to 1-D. Use flatten when you need a safe copy you will modify.

How it works

  1. Array with shape (6,)
  2. Does rows x cols equal arr.size? Yes: reshape(rows, cols) fills row by row. No: ValueError: cannot reshape.
  3. Use -1 to let NumPy compute one dimension
  4. arr.T swaps rows and columns
  5. Need 1-D again? Yes: ravel() for a view, flatten() for a copy. No: Keep the 2-D shape.
  6. Array in the shape the next step needs

Syntax

arr.shape
arr.reshape(rows, cols)
arr.reshape(-1, 1)
arr.ravel()  arr.flatten()  arr.T

Example

a = np.arange(1, 7)
m = a.reshape(2, 3)
print(m.T.shape)   # (3, 2)

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