How do I check if a pandas DataFrame is empty?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The most direct way to check whether a pandas DataFrame is empty is to use the empty attribute. That answer is correct for the usual structural meaning of empty, but pandas uses a precise definition that sometimes surprises people. A frame with no rows or no columns is empty, while a frame full of NaN values is not empty if its shape still has rows and columns.
Use df.empty
Here is the normal solution:
df.empty returns True if any axis has length 0. In practice, that means:
- A frame with zero rows is empty.
- A frame with zero columns is empty.
- A frame with both zero rows and zero columns is empty.
This is usually exactly what you want before processing, exporting, or iterating over a dataset.
Empty Does Not Mean "All Values Are NaN"
A common surprise is that a frame full of missing values is not considered empty if it still has rows and columns:
That result is correct. The frame contains one row, even though the row has no useful values.
If your real requirement is "contains no non-missing data", combine dropna with empty:
This checks a different condition from structural emptiness, so name your helper functions accordingly.
Alternative Checks
You can also check the number of rows directly:
Or:
These checks are fine when your definition of empty specifically means "no rows." They are slightly narrower than df.empty, which also returns True for zero-column frames.
For example:
This frame has rows in its index but no columns, so pandas still treats it as empty.
Use The Right Check For The Pipeline
Different data pipelines need different emptiness checks:
- Before
to_csv,df.emptyis often enough. - Before model training, you may care specifically about zero rows.
- Before analytics, you may need to exclude rows that are all missing.
A small helper can make that intent explicit:
That is often clearer than one generic is_empty helper with ambiguous semantics.
Example In A Real Workflow
Suppose you load query results from a database and want to skip downstream processing when nothing came back:
This avoids unnecessary work and helps you return early from ETL or reporting jobs.
Common Pitfalls
- Writing
if df:and expecting normal container truthiness. Fix: usedf.emptyor an explicit shape-based condition. - Assuming
df.emptymeans the frame has no useful data. Fix: usedropna(...).emptyor another data-quality check when that is the real requirement. - Using
len(df) == 0without thinking about zero-column frames. Fix: decide whether you care about row count only or structural emptiness. - Hiding different emptiness rules behind one vague helper name. Fix: name helpers after the exact condition they test.
Summary
- Use
df.emptyfor the standard pandas emptiness check. - '
df.emptyis about structure, not whether values are meaningful.' - A frame full of
NaNvalues is not empty if it still has rows and columns. - Use
shape[0] == 0orlen(df.index) == 0when you specifically mean "no rows." - Choose the emptiness rule that matches the actual pipeline requirement.

