Pandas >> how to Check if Dataframe Is Empty
Table of Contents
In this article, we will talk about how to check if the dataframe is empty in Pandas
.
Firstly, we prepare a empty dataframe.
import pandas as pd
df = pd.DataFrame(columns=["id", "name", "score"])
df
Result
id | name | score |
---|
Use empty attribute of DataFrame to check
# We can also use empty attribute of DataFrame to check if dataframe is empty
if df.empty:
print("dataframe is empty")
else:
print("dataframe is not empty")
Result
dataframe is empty
Use len(df) to check (faster)
len(df)
Result
0
# check if dataframe is empty by checking len(df) == 0
if len(df) == 0:
print("dataframe is empty")
else:
print("dataframe is not empty")
Result
dataframe is empty
Use len(df.index) to check (even faster)
len(df.index)
Result
0
# check if dataframe is empty by checking len(df.index) == 0
if len(df.index) == 0:
print("dataframe is empty")
else:
print("dataframe is not empty")
Result
dataframe is empty