Python | Pandas DataFrame.empty

Python Methods and Functions

DataFrame.empty Pandas DataFrame.empty checks if is DataFrame. empty data empty or not. It returns True if the info frame is empty, otherwise it returns False .

Syntax: DataFrame.empty

Parameter: None

Returns: bool

Example # 1: Use the DataFrame.empty attribute to check if a given data frame is empty or not.

# import pandas as pd

import pandas as pd

 
# Create DataFrame

df = pd.DataFrame ({ `Weight` : [ 45 , 88 , 56 , 15 , 71 ],

`Name` : [ `Sam` , ` Andrea` , `Alex` , ` Robin` , `Kia` ],

  ` Age` : [ 14 , 25 , 55 , 8 , 2 1 ]})

 
# Create Index

index_ = [ `Row_1` , ` Row_2` , `Row_3` , ` Row_4` , `Row_5` ]

 
# Set index

df .index = index_

 
# Print DataFrame

print ( df)

  Output:

Now we will use the DataFrame attribute .empty to check if the given data frame is empty or not.

# check if there is an element
# or not in the given data frame

result = df.empty

 
# Print result

print (result)

Output:

As we can see from the output, the DataFrame.empty attribute in Returns False indicating that this data frame is not empty.

Example # 2: Use the DataFrame.empty attribute to check if the given data frame is empty or not.

# import pandas as pd

import pandas as pd

 
# Create an empty DataFrame

df = pd.DataFrame (index = [ `Row_1 ` , ` Row_2` , `Row_3` , ` Row_4` , `Row_5` ])

 
# Print DataFrame

print (df)

Output:

We will now use the DataFrame attribute. empty to check if the given data frame is empty or not.

# check if there is an element
# or not in the given data frame

result = df.empty

 
# Print result tat

print (result)

Output:

As we can see in the output, the DataFrame.empty attribute returned True indicating that this data frame is empty.



Get Solution for free from DataCamp guru