Python Pandas: How to Get Row and Column Counts in Pandas
Knowing the dimensions of a DataFrame is one of the most basic and frequently needed operations in data analysis. Whether you are validating that a data import loaded the expected number of records, checking how many features a dataset contains, or writing conditional logic based on dataset size, Pandas provides several efficient methods for retrieving row and column counts.
In this guide, you will learn the different ways to get DataFrame dimensions, understand when to use each approach, and avoid a common mistake that returns something entirely different from what you expect.
Getting Both Dimensions with .shape
The .shape property is the most idiomatic way to get DataFrame dimensions. It returns a tuple of (rows, columns):
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
})
rows, cols = df.shape
print(f"Dataset: {rows} rows x {cols} columns")
# Access individually by index
print(f"Rows: {df.shape[0]}")
print(f"Columns: {df.shape[1]}")
Output:
Dataset: 3 rows x 3 columns
Rows: 3
Columns: 3
This is the recommended approach when you need both dimensions or want a single, consistent way to access either one.
Getting Row Count Only
Use len() for the fastest and most readable row count:
import pandas as pd
df = pd.DataFrame({'A': range(1000)})
num_rows = len(df)
print(f"Rows: {num_rows}")
Output:
Rows: 1000
len(df) is the most Pythonic way to ask "how many rows does this DataFrame have?" and is slightly faster than df.shape[0] for this single purpose.
Getting Column Count Only
Check the length of the columns index:
import pandas as pd
df = pd.DataFrame({'A': [1], 'B': [2], 'C': [3]})
num_cols = len(df.columns)
print(f"Columns: {num_cols}")
Output:
Columns: 3
You can also use df.shape[1], but len(df.columns) makes the intent clearer when you specifically want to know the number of columns.
Common Mistake: Using count() for Dimensions
A frequent error is using .count() to get the number of rows. This method does something entirely different: it counts non-null values per column, not the total number of rows:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'A': [1, np.nan, 3],
'B': [4, 5, np.nan]
})
# Wrong: count() returns non-null values per column
print("df.count():")
print(df.count())
print()
# Correct: shape gives actual dimensions
print(f"df.shape: {df.shape}")
print(f"len(df): {len(df)}")
Output:
df.count():
A 2
B 2
dtype: int64
df.shape: (3, 2)
len(df): 3
df.count() counts non-null values per column, not total rows. It returns a Series rather than a single number, it is significantly slower than len(df) or df.shape, and it gives a different result when your data contains missing values. Always use len(df) or df.shape[0] for row counts.
Practical Examples
Validating Data After Import
import pandas as pd
df = pd.read_csv('sales_data.csv')
rows, cols = df.shape
print(f"Loaded {rows} records with {cols} features")
if rows == 0:
print("Warning: No data loaded!")
Conditional Logic Based on Size
import pandas as pd
df = pd.DataFrame({'A': range(100), 'B': range(100)})
if len(df) > 50:
print("Large dataset: using sampling for preview")
print(df.sample(5))
else:
print("Small dataset: displaying all rows")
print(df)
Output:
Large dataset: using sampling for preview
A B
72 72 72
86 86 86
35 35 35
36 36 36
63 63 63
Calculating Total Cells and Memory
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(1000, 50))
rows, cols = df.shape
total_cells = rows * cols
print(f"Dimensions: {rows} rows x {cols} columns")
print(f"Total cells: {total_cells:,}")
print(f"Memory usage: {df.memory_usage(deep=True).sum() / 1024:.1f} KB")
Output:
Dimensions: 1000 rows x 50 columns
Total cells: 50,000
Memory usage: 390.8 KB
Quick Reference
| Goal | Method | Returns |
|---|---|---|
| Both dimensions | df.shape | Tuple (rows, cols) |
| Row count | len(df) | Integer |
| Row count (alt) | df.shape[0] | Integer |
| Column count | len(df.columns) | Integer |
| Column count (alt) | df.shape[1] | Integer |
- Use
df.shapeto get both dimensions as a tuple. Uselen(df)for quick row counts in conditionals, loops, or print statements. - Use
len(df.columns)when you specifically need the column count. - Avoid
df.count()for dimension checks, as it calculates non-null values per column and serves a completely different purpose.