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

Pandas · Pipe

df.shape

DataFrame.shape returns a tuple containing the number of rows and columns in that order. Read it as (nrows, ncolumns) without calling it.

How it works

DataFrame.shape is a property, so access it without parentheses. It returns a two-item tuple whose first value is the number of rows and whose second value is the number of columns; the tuple updates when you inspect a selected DataFrame.

Syntax

DataFrame.shape

Returns

Tuple of array dimensions.

Key points

  • The property describes the DataFrame's dimensions by returning a tuple.
  • The returned value is tuple-like dimensional metadata, rather than a new DataFrame or a scalar.
  • shape reports both dimensions, while len(df) reports only the row count.

Example: Inspect DataFrame dimensions

import pandas as pd

df = pd.DataFrame({"name": ["Ada", "Linus"], "score": [98, 95]})
rows, columns = df.shape
print(rows, columns)
print(df[["name"]].shape)

Reads the DataFrame dimensions and shows that selecting one column changes the column count.

When to use it

Check dataset dimensions

Validate an imported or transformed table before further processing.

Verify selection results

Confirm that filtering or column selection produced the expected size.

Drive dimension-aware code

Unpack row and column counts for conditional logic or logging.

Interview questions

QWhat does DataFrame.shape return?

It is a property that returns a tuple describing DataFrame dimensionality. For a DataFrame, the tuple is ordered as (number of rows, number of columns).

Follow-up: How would you get only the row count? Use df.shape[0]; len(df) also gives the number of rows, but not the column count.

QHow do you interpret the two values in the returned tuple?

A two-row, two-column DataFrame has df.shape == (2, 2). The first component represents rows and the second represents columns.

Follow-up: What happens after selecting one column? A DataFrame selection such as df[["name"]] remains two-dimensional and reports (rows, 1).

QHow is shape accessed in pandas?

Access the property directly: df.shape. Parentheses are not used because it is an attribute, not a method.

Follow-up: Why might df["name"].shape differ from df[["name"]].shape? The single-label form returns a Series, whereas the list form preserves a DataFrame.

  • DataFrame.ndim
  • DataFrame.size
  • DataFrame.index
  • DataFrame.columns
  • Series.shape

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