When working with data, there’s a need to verify the state of the DataFrame before manipulating the data or after applying filters to the actual data. The DataFrame.is_empty()
fucntion checks whether the DataFrame contains data or is empty, which helps us ensure that the data is available for manipulation and other tasks. In this Answer, we’ll explore how to use the DataFrame.is_empty()
function.
DataFrame.is_empty()
functionThe is_empty()
function in Polars checks whether a DataFrame contains any rows or data. It returns true
if the DataFrame is empty (contains no rows) and false
if it has at least one row of data.
Here’s the syntax for the is_empty()
function:
DataFrame.is_empty()
This function doesn’t require any parameters, and it’s applied to the given DataFrame
to check whether it’s empty.
The is_empty()
function returns a boolean value, i.e., true
or false
.
In the code widget below, we have two Polars DataFrames. One has data on students and their marks in different subjects, and the other is empty. We apply the is_empty()
method on both DataFrames and print the acquired output.
import polars as pldf = pl.DataFrame({"Students": ["Joseph", "Danial", "Ema", "John"],"Calculus": [98, 85, 92, 67],"Data structures": [91, 89, 92, 55],"Operating system": [96, 88, 91, 62],})empty_df = pl.DataFrame([])print("Is df DataFrame empty?", df.is_empty())print("Is empty_df DataFrame empty?", empty_df.is_empty())
Let’s discuss the above code in detail:
Line 1: We import the polars
library as pl
.
Lines 2–9: We define the Polars DataFrame as df
for the students’ score reports for calculus, data structures, and operating system courses.
Line 11: We define another empty DataFrame named empty_df
.
Lines 13–14: We apply the is_empty()
function on both DataFrames and print the result.
DataFrame is a very popular data structure used commonly for data analysis and manipulation tasks. The Polars library provides a lightweight and fast mechanism to work with DataFrames. In this Answer, we saw the working of the is_empty()
function to check the emptiness of DataFrames.
Free Resources