Say you’ve pulled eight January 2026 web orders into Python, and before you build the sales report you need to get length of DataFrame in pandas. Call len(df); it returns 8, the number of rows.
But “length” can also mean columns, cells or non-blank values, and mixing them up is how a report ends up saying 40 orders instead of 8.
In this tutorial, I’ll show you how to get length of DataFrame in pandas with len(), shape, index, size and count(), then count rows that match a condition or fall into groups.
The Sample Orders DataFrame We’ll Measure
Every example reads the same small file, orders.csv, with eight online orders from US customers. I ran the code with Python 3.12.5 and pandas 3.0.6 on Windows.
order_id,customer,state,amount,shipped
5001,Megan Turner,TX,249.99,2026-01-08
5002,Jason Brooks,WA,89.50,2026-01-09
5003,Emily Carter,NY,,
5004,Marcus Reed,CA,1299.00,2026-01-12
5005,Laura Bennett,TX,54.25,2026-01-12
5006,Kevin Price,IL,310.75,
5007,Rachel Hayes,CA,,2026-01-15
5008,Tyler Morgan,TX,799.99,2026-01-16
Each script starts by loading it with pd.read_csv(); my pandas read_csv tutorial covers the options. Here’s the DataFrame you get:
import pandas as pd
df = pd.read_csv("orders.csv")
print(df)
Output:
order_id customer state amount shipped
0 5001 Megan Turner TX 249.99 2026-01-08
1 5002 Jason Brooks WA 89.50 2026-01-09
2 5003 Emily Carter NY NaN NaN
3 5004 Marcus Reed CA 1299.00 2026-01-12
4 5005 Laura Bennett TX 54.25 2026-01-12
5 5006 Kevin Price IL 310.75 NaN
6 5007 Rachel Hayes CA NaN 2026-01-15
7 5008 Tyler Morgan TX 799.99 2026-01-16
Two rows are tricky on purpose. Orders 5003 and 5007 have no amount, and orders 5003 and 5006 haven’t shipped. Those blanks are exactly what makes len() and count() disagree later.
Method 1: Get the DataFrame Length with len() (Recommended)
Use len() when you only need the row count.
Same orders DataFrame as above. Here’s the code:
import pandas as pd
df = pd.read_csv("orders.csv")
print(len(df))
print(f"Orders loaded: {len(df)}")
if len(df) < 10:
print("Small batch: review it by hand")
print(len(pd.DataFrame()))
Output:
8
Orders loaded: 8
Small batch: review it by hand
0

len(df) returns 8 for the orders, the check prints the small-batch message, and an empty DataFrame returns 0.How does this code work?
A DataFrame’s length is the length of its row index, so len(df) counts rows and ignores columns completely. Blank values don’t matter either: order 5003 still counts as a row. My guide to the len() function in Python explains how other objects define their length.
Method 2: Get Rows and Columns Together with the shape Attribute
Reach for shape when you want both dimensions. It’s an attribute, not a method, so there are no parentheses.
Same eight orders, now measured both ways:
import pandas as pd
df = pd.read_csv("orders.csv")
print(df.shape)
rows, cols = df.shape
print(f"{rows} rows x {cols} columns")
print(df.shape[0], df.shape[1])
Output:
(8, 5)
8 rows x 5 columns
8 5

df.shape returns the tuple (8, 5), which unpacks into 8 rows and 5 columns.How does this code work?
shape is a (rows, columns) tuple. Index 0 is the row count, which always matches len(df), and index 1 is the column count. Unpacking it into two names is the cleanest way to print both.
Pro Tip: I print df.shape right after every load and every filter. It’s the quickest way to catch a bad join or a filter that dropped too much, before you look at the first few rows.
Method 3: Count Rows and Columns with len(df.index) and len(df.columns)
Some people prefer len(df.index) because it says exactly what it measures. Its partner, len(df.columns), gives the column count.
import pandas as pd
df = pd.read_csv("orders.csv")
print(len(df.index))
print(len(df.columns))
print(df.columns.tolist())
Output:
8
5
['order_id', 'customer', 'state', 'amount', 'shipped']
How does this code work?
df.index holds the row labels and df.columns holds the column names, so their lengths are 8 and 5. If you need the names themselves, see how to get pandas column names.
Method 4: Count DataFrame Cells and Non-Null Values with size and count()
size is the total number of cells, while count() is the number of non-blank values in each column.
import pandas as pd
df = pd.read_csv("orders.csv")
print(df.size)
print(len(df) * len(df.columns))
Output:
40
40
size is rows times columns, so 8 x 5 = 40. Now count() on the same orders, with two missing amounts and two missing ship dates:
import pandas as pd
df = pd.read_csv("orders.csv")
print(df.count())
Output:
order_id 8
customer 8
state 8
amount 6
shipped 6
dtype: int64

count() finds all 8 values in three columns but only 6 in amount and shipped.How does this code work?
count() skips NaN and None, so the amount and shipped columns each report 6 instead of 8.
Important: don’t use count() as a row count; it returns one number per column and shrinks whenever data is missing. To fix those blanks first, see how to drop rows with NaN in pandas.
How Do You Get the Length of a pandas Series?
A single column is a Series, and the same tools work on it. Here’s the amount column:
import pandas as pd
df = pd.read_csv("orders.csv")
amount = df["amount"]
print(len(amount), amount.size, amount.shape)
print(amount.count(), amount.isna().sum())
Output:
8 8 (8,)
6 2
len() and size both return 8, and shape is a one-item tuple. count() returns 6, and isna().sum() confirms the other 2 are missing.
Method 5: Get the DataFrame Length for Rows That Match a Condition
Often you don’t want every row, just the ones that pass a test, such as orders from Texas. Same DataFrame; here’s the code:
import pandas as pd
df = pd.read_csv("orders.csv")
tx = df[df["state"] == "TX"]
print(len(tx))
print((df["state"] == "TX").sum())
print(df.query("amount > 200").shape[0])
print(df["shipped"].isna().sum())
Output:
3
3
4
2

How does this code work?
df[df["state"] == "TX"] keeps the three Texas rows, and len() counts them. Summing the Boolean mask gives the same 3 without building a new DataFrame, because each True counts as 1.
query() plus shape[0] reads well for longer conditions. The two blank amounts are never greater than 200, so only 4 orders pass. My guide on counting rows in a pandas DataFrame with conditions has more patterns.
Method 6: Get the Number of Rows per Group
To get a length for each state at once, group first. Same orders, grouped by state:
import pandas as pd
df = pd.read_csv("orders.csv")
print(df.groupby("state").size())
Output:
state
CA 2
IL 1
NY 1
TX 3
WA 1
dtype: int64
How does this code work?
groupby().size() returns the row count of each group, so Texas has 3 and California 2. value_counts() returns the same counts, sorted from largest. For sums and averages per group, see my pandas groupby aggregation guide.
How Do You Check Whether a DataFrame Has No Rows?
A filter can return zero rows, and code that expects data then fails. Here’s a filter for Florida, where we have no orders:
import pandas as pd
df = pd.read_csv("orders.csv")
fl = df[df["state"] == "FL"]
print(len(fl), fl.shape, fl.empty)
Output:
0 (0, 5) True
The Florida result has length 0 and shape (0, 5), and empty is True. More options are in checking if a pandas DataFrame is empty.
Which Method Should You Use to Get DataFrame Length?
| You need | Use | Result on the orders |
|---|---|---|
| Number of rows | len(df) | 8 |
| Rows and columns | df.shape | (8, 5) |
| Number of columns | len(df.columns) or df.shape[1] |