Check dataset dimensions
Validate an imported or transformed table before further processing.
Pandas · Pipe
DataFrame.shape returns a tuple containing the number of rows and columns in that order. Read it as (nrows, ncolumns) without calling it.
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.
DataFrame.shapeTuple of array dimensions.
shape reports both dimensions, while len(df) reports only the row count.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.
Validate an imported or transformed table before further processing.
Confirm that filtering or column selection produced the expected size.
Unpack row and column counts for conditional logic or logging.
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.
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).
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.ndimDataFrame.sizeDataFrame.indexDataFrame.columnsSeries.shape