What is the DataFrame.clear() function in Polars Python?
Polars, a Rust-based library, outperforms pandas for large data manipulation, especially with tabular data. It offers a high-speed DataFrame for efficient slicing, filtering, and transformations in Python and Rust.
The clear() function
The DataFrame.clear() method generates a copy of the null-filled DataFrame. It accepts a value as a parameter, e.g.,
Syntax
Here’s the syntax of the DataFrame.clear() function:
Dataframe.clear(n)
Here n represents the number of rows filled with null values in the cleared frame.
Code example
Let’s have a look at a coding example of clearing the Dataframe using clear() method in polars:
import polars as pldf = pl.DataFrame({"Country": ["Japan", "Singapore", "Indonesia", "Italy", "France"],"City": ["Osaka", "Tengah", "Medan", "Rome", "Paris"],"Salary": [10000, 85676, 367576, 18939, None],"Role": ["Engineer", "Doctor", None , None, "Chef"],})# Return an empty Dataframeprint(df.clear())# Return a Dataframe with 2 rowsprint(df.clear(n = 2))# Return a Dataframe with 7 rows(more than the original Dataframe)print(df.clear(n = 7))
Explanation
In the above code:
Line 1: We import the
polarslibrary aspl.Lines 3–10: We define our DataFrame as
df, which includesCountry,City,Salary, andRole.Lines 13–17: We implement the
clear()method to the created Dataframe, passing different arguments, resulting in an empty DataFrame, a DataFrame with2rows, and another DataFrame containing more than the defined rows (7) in the original DataFrame.
Conclusion
In conclusion, the DataFrame.clear() function in polars Python generates a null-filled DataFrame copy. It produces an empty DataFrame for
Unlock your potential: Polars in Python series, all in one place!
To continue your exploration of Polars, check out our series of Answers below:
How to scale and normalize data in Python using Polars
Learn how to transform raw data using Python's Polars library to scale it (0-1) and normalize it (mean 0, std 1).What is DataFrame.clear function in Polars Python?
Learn how to use Polars'DataFrame.clear()to create a null-filled copy, either empty ifn=0or withnnull rows.How to reverse a DataFrame in Polars Python?
Learn how to use Polars, a Rust-based DataFrame library for Python, which offers areverse()function to efficiently revert DataFrame rows, providing an alternative to pandas.How to rename the column names in Polars Python?
Learn how to use Polars'rename()function to efficiently rename DataFrame columns using key-value pairs, enhancing data management and processing.What is Polars library in Python?
Learn how Polars, a fast DataFrame library in Rust for Python, offers high-performance data manipulation and analysis similar to Pandas.How to concatenate two Dataframes in Polars Python
Learn how Polars, leveraging Rust, offers efficient DataFrame concatenation in Python with theconcat()method.How to perform a transpose of a Python Polars DataFrame
Learn how to use Polars'DataFrame.transpose()to efficiently transpose DataFrames, with options for including headers and custom column names, enhancing data manipulation capabilities.How to check the polars version in Python
Learn how to ensure the correct Polars version by usingpip3 show polarsor by printingpl.__version__in Python.What is DataFrame.update function in Polars Python?
Learn how to use theupdate()function in Polars to merge two DataFrames, updating the target with non-null values from the source, and supporting various join strategies.
Free Resources