Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Module

df.squeeze

Remove DataFrame axes of length one. Depending on the shape and selected axis, the result is a DataFrame, Series, or scalar.

How it works

Call squeeze to remove axes of length one. Selecting a single column can yield a Series, selecting a single row can yield a Series, and reducing both dimensions of a one-cell DataFrame yields a scalar; objects without singleton axes remain unchanged.

Syntax

DataFrame.squeeze(axis=None)

Parameters

NameTypeDescription
axis{0 or 'index', 1 or 'columns', None}, default NoneAxis to squeeze: 0 or "index", 1 or "columns", or None; defaults to None. With None, all length-one axes are squeezed.

Returns

DataFrame, Series, or scalar — the projection after squeezing the selected axis or all axes.

Key points

  • The method removes axes with length one rather than changing values or labels on non-singleton axes.
  • A one-element Series or DataFrame reduces directly to its contained scalar.
  • A DataFrame with exactly one column or one row becomes a Series when that singleton axis is squeezed.
  • Inputs with no length-one axis retain their original object dimensionality.
  • Pass an axis selector to constrain reduction to the index or columns dimension.

Common mistakes

  • The axis argument does not affect Series inputs; leave it as None.
  • Omitting axis can reduce every singleton dimension, so the result may be a DataFrame, Series, or scalar depending on shape.

Example: Squeeze singleton dimensions

import pandas as pd

df = pd.DataFrame([[1], [3]], columns=["a"])
series = df.squeeze("columns")
value = pd.DataFrame([[1]]).squeeze()

print(type(series).__name__)
print(value)

A one-column DataFrame becomes a Series when the columns axis is squeezed, while a one-cell DataFrame becomes its scalar value.

When to use it

Convert a singleton column selection to a Series

A pipeline selects one column but retains DataFrame shape.

Extract a single scalar

A filtered table contains one row and one column.

Normalize dimensionality

Code accepts either a Series or DataFrame with a known singleton dimension.

Interview questions

QWhat does DataFrame.squeeze return?

DataFrame.squeeze removes dimensions whose length is one. A one-column or one-row DataFrame becomes a Series, while a one-element object becomes a scalar; otherwise the object is unchanged.

Follow-up: What does axis="columns" do? It targets the columns axis, so a DataFrame with exactly one column can be reduced to a Series without squeezing a singleton row axis.

QWhat is the axis parameter?

The signature is DataFrame.squeeze(axis=None). axis accepts 0 or "index", 1 or "columns", or None; None squeezes all length-one axes.

Follow-up: Why might explicit axis selection be preferable? It limits the dimensionality change to the intended axis and avoids accidentally reducing a separate singleton dimension.

QHow does the output type depend on input shape?

The return type is a DataFrame, Series, or scalar, representing the projection after squeezing the selected axis or all axes. Therefore callers should account for shape-dependent output types.

Follow-up: How can code normalize a known single-column selection? Select the column while retaining DataFrame shape, then call squeeze("columns") to obtain a Series.

  • DataFrame.loc
  • DataFrame.__getitem__
  • Series.squeeze

Source: pandas.DataFrame.squeeze. Checked 2026-10-15.