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.
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
print(df.shape)2Practical Example
Here is a real-world application of df.shape showing how it is used in production Pandas code.
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]}")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.
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
print(df.shape)