🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.shape

AI & DATA SCIENCE // df-shape

df.shape is a tuple giving the DataFrame's dimensions as (number_of_rows, number_of_columns).

Syntax

df.shape

Deep Dive Course

shape is an attribute, not a method — you access it directly without parentheses, the same convention as a NumPy array's .shape, since a DataFrame is fundamentally a 2D structure under the hood. It's the fastest way to check a DataFrame's size, and is commonly used to sanity-check the result of an operation, like verifying a merge or filter produced the expected number of rows.

1Understanding df.shape

shape is an attribute, not a method — you access it directly without parentheses, the same convention as a NumPy array's .shape, since a DataFrame is fundamentally a 2D structure under the hood. It's the fastest way to check a DataFrame's size, and is commonly used to sanity-check the result of an operation, like verifying a merge or filter produced the expected number of rows.

💡

Remember df.shape has no parentheses — it's an attribute, not a method call, the same as a NumPy array's .shape; writing it as a function call raises a TypeError since a tuple isn't callable.

editor.html
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
print(df.shape)
localhost:3000

2Practical Example

Here is a real-world application of df.shape showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"a": range(100), "b": range(100)})
filtered = df[df["a"] > 50]
print(f"Rows: {filtered.shape[0]}, Columns: {filtered.shape[1]}")
localhost:3000

3Best Practices

Follow these guidelines when working with df.shape:

1. Check df.shape immediately after operations like merging, filtering, or dropping rows/columns, to quickly verify the result matches expectations

2. Access shape[0] for row count or shape[1] for column count directly, instead of calling len(df) and len(df.columns) separately

3. Remember shape is an attribute, no parentheses, unlike most DataFrame inspection methods like head() or describe()

⚠️

Tip: Remember df.shape has no parentheses — it's an attribute, not a method call, the same as a NumPy array's .shape; writing it as a function call raises a TypeError since a tuple isn't callable.

editor.html
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
print(df.shape)
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
print(df.shape)
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"a": range(100), "b": range(100)})
filtered = df[df["a"] > 50]
print(f"Rows: {filtered.shape[0]}, Columns: {filtered.shape[1]}")

Best Practices

  • Check df.shape immediately after operations like merging, filtering, or dropping rows/columns, to quickly verify the result matches expectations
  • Access shape[0] for row count or shape[1] for column count directly, instead of calling len(df) and len(df.columns) separately
  • Remember shape is an attribute, no parentheses, unlike most DataFrame inspection methods like head() or describe()

Interview Question

Why does df.shape[0] give the number of rows rather than the number of columns?

Hint: Think about how shape's tuple ordering matches NumPy's array convention.

shape follows the same convention as a NumPy array's shape tuple, where each position corresponds to a dimension, and for a DataFrame, the first dimension, axis 0, is the row axis, and the second, axis 1, is the column axis. So shape[0] is the size along the row axis, the row count, and shape[1] is the size along the column axis, the column count — consistent with how axis numbering works throughout pandas and NumPy more broadly, like the axis argument in functions such as sum() or mean().

Exercises

MediumPractice using df.shape in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
print(df.shape)

Frequently Asked Questions

Why does df.shape[0] give the number of rows rather than the number of columns?

shape follows the same convention as a NumPy array's shape tuple, where each position corresponds to a dimension, and for a DataFrame, the first dimension, axis 0, is the row axis, and the second, axis 1, is the column axis. So shape[0] is the size along the row axis, the row count, and shape[1] is the size along the column axis, the column count — consistent with how axis numbering works throughout pandas and NumPy more broadly, like the axis argument in functions such as sum() or mean().

Related Functions

df-dtypesndarray-shapedf-columns