• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Pandas 101 / How to Find if Pandas Dataframe is empty

How to Find if Pandas Dataframe is empty

September 15, 2022 by cmdlinetips

In this tutorial, we will learn how to find if a Pandas dataframe is empty or not. Pandas empty method applied to a Pandas dataframe will tell us if the dataframe is empty or not. Pandas’ empty method will return True if the dataframe is empty and False otherwise.

Let us see some examples of using Pandas empty method. First we will load Pandas.

import pandas as pd

Applying Pandas empty method on non-empty dataframe

Here we create toy dataframe with some data in it.

df1 = pd.DataFrame({"col1":["A","B","C"], 
                   "col2":[1,2,3]})

We can see that our dataframe is not empty.

df1
     col1    col2
0	A	1
1	B	2
2	C	3

And apply Pandas empty method on this dataframe we should get False as result.

df1.empty

False

Applying Pandas empty method on empty dataframe

Now let us create an empty dataframe with just one column using Pandas DataFrame() function.

# create an empty dataframe
df2 = pd.DataFrame({"col1":[]})

When we check the content of the dataframe, we get nothing as it is an empty dataframe.

df2

col1

By apply empty function on this dataframe we should get True as result as it is an empty dataframe.

df2.empty

True

Note that a dataframe with missing value is not an empty dataframe. Here is an illustration of applying empty method to a datframe with missing value.

# Create a dataframe with a missing value
df3 = pd.DataFrame({"col1":[pd.NA]})
df3


col1
0	<NA>

Correctly, we get False as result if we apply Pandas empty method.

df3.empty

False

Our result will change if we drop the NAs and check if it is empty or not.

df3.dropna.empty

True

Pandas empty method is often useful sanity check after some series of filtering step in doing data analysis. Here is an example where after filtering out for missing values a dataframe becomes empty.

In this toy example every row has one NA.

df = pd.DataFrame({"col1":[pd.NA,"B",pd.NA], 
                   "col2":[1,pd.NA,3]})
df


     col1	col2
0	<NA>	1
1	B	<NA>
2	<NA>	3

And after dropping NAs with dropna() function it becomes empty. And if we check using Pandas empty method, we will correctly get True as answer.

df.dropna().empty

True

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow To Compare Two Dataframes with Pandas compare? Default ThumbnailHow To Add Identifier Column When Concatenating Pandas data frames? Default ThumbnailHow To Concatenate Two or More Pandas DataFrames? Default ThumbnailHow to Filter a Pandas Dataframe Based on Null Values of a Column?

Filed Under: Pandas 101, Python Tips Tagged With: Pandas empty

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version