Built with Tutify.io

    AIStudyLabs

    Polars DataFrame clear() Usage & Examples

    Polars DataFrame clear() Usage & Examples

    The polars.DataFrame.clear() method is used to remove all rows from a Polars DataFrame while preserving the column structure and data types. This effectively resets the DataFrame to an empty state without losing its schema.

    In this article, I will explain the Polars DataFrame clear() function, covering its syntax, parameters, and usage. I will also demonstrate how it creates a new DataFrame that retains the same column structure and data types while keeping either zero rows (by default) or the first n rows.

    Key Points –

    <a id="h-polars-dataframe-clear-introduction"></a>

    Polars DataFrame clear() Introduction

    Let’s know the syntax of the clear() function.

    
    # Syntax of polars clear()
    DataFrame.clear(n: int = 0) → DataFrame
    

    <a id="h-parameters-of-the-polars-dataframe-clear"></a>

    Parameters of the Polars DataFrame clear()

    It allows only one parameter.

    <a id="h-return-value"></a>

    Return Value

    This function returns a new DataFrame with either zero rows (default) or first n rows retained. The column structure and data types remain unchanged.

    <a id="h-usage-of-polars-dataframe-clear-method"></a>

    Usage of Polars DataFrame clear() Method

    The clear() method in Polars removes all rows from a DataFrame while preserving its schema (column names and data types). It returns a new empty DataFrame with the same structure as the original, but containing no data.

    First, let’s create a Polars DataFrame.

    
    import polars as pl
    
    technologies= {
        'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
        'Fees' :[22000,25000,23000,24000,26000],
        'Duration':['30days','50days','35days', '40days','55days'],
        'Discount':[1000,2300,1000,1200,2500]
              }
    df = pl.DataFrame(technologies)
    print("Original DataFrame:\n", df)
    

    Yields below output.

    You can use the clear() method to remove all rows from a DataFrame while preserving its column structure and data types. This method ensures that the DataFrame remains empty while maintaining its original schema.

    
    # Remove all rows
    df2 = df.clear()
    print("DataFrame after clear():\n", df2)
    

    Here,

    <a id="h-keeping-the-first-2-rows-n-2"></a>

    Keeping the First 2 Rows (n=2)

    The clear(n) method in Polars removes all rows except the first n rows, allowing you to retain a subset of the data while clearing the rest.

    
    # Keeping only the first 2 rows
    df2 = df.clear(n=2)
    print("DataFrame after clear(n=2):\n", df2)
    
    # Output:
    # DataFrame after clear(n=2):
    # shape: (2, 4)
    ┌─────────┬──────┬──────────┬──────────┐
    │ Courses ┆ Fees ┆ Duration ┆ Discount │
    │ ---     ┆ ---  ┆ ---      ┆ ---      │
    │ str     ┆ i64  ┆ str      ┆ i64      │
    ╞═════════╪══════╪══════════╪══════════╡
    │ null    ┆ null ┆ null     ┆ null     │
    │ null    ┆ null ┆ null     ┆ null     │
    └─────────┴──────┴──────────┴──────────┘
    

    Here,

    <a id="h-using-clear-with-conditional-operations"></a>

    Using clear() with Conditional Operations

    The clear(n) method in Polars allows you to remove rows while optionally keeping the first n rows. However, if you want to conditionally clear specific rows, you need to combine clear() with filtering techniques.

    Since clear(n) does not directly support conditions, you can filter the DataFrame first and then apply clear(n).

    
    # Filtering rows where 'Fees' > 24000
    filtered_df = df.filter(df['Fees'] > 24000)
    print("Filtered DataFrame (Fees > 24000):\n", filtered_df)
    
    # Keeping only the first 2 rows from the filtered DataFrame
    df2 = filtered_df.clear(n=2)
    print("DataFrame after clear(n=2) on filtered data:\n", df2)
    
    # Output:
    # Filtered DataFrame (Fees > 24000):
    # shape: (2, 4)
    ┌─────────┬───────┬──────────┬──────────┐
    │ Courses ┆ Fees  ┆ Duration ┆ Discount │
    │ ---     ┆ ---   ┆ ---      ┆ ---      │
    │ str     ┆ i64   ┆ str      ┆ i64      │
    ╞═════════╪═══════╪══════════╪══════════╡
    │ PySpark ┆ 25000 ┆ 50days   ┆ 2300     │
    │ Pandas  ┆ 26000 ┆ 55days   ┆ 2500     │
    └─────────┴───────┴──────────┴──────────┘
    # DataFrame after clear(n=2) on filtered data:
    # shape: (2, 4)
    ┌─────────┬──────┬──────────┬──────────┐
    │ Courses ┆ Fees ┆ Duration ┆ Discount │
    │ ---     ┆ ---  ┆ ---      ┆ ---      │
    │ str     ┆ i64  ┆ str      ┆ i64      │
    ╞═════════╪══════╪══════════╪══════════╡
    │ null    ┆ null ┆ null     ┆ null     │
    │ null    ┆ null ┆ null     ┆ null     │
    └─────────┴──────┴──────────┴──────────┘
    

    Here,

    <a id="h-checking-if-a-dataframe-is-empty-after-clear"></a>

    Checking If a DataFrame is Empty After clear()

    When you apply the clear() method to a DataFrame, it removes all rows while keeping the column structure. You can check if the DataFrame is empty after using clear() function with the is_empty() method.

    
    # Clearing the DataFrame
    df2 = df.clear()
    print("DataFrame after clear():\n", df2)
    
    # Checking if the DataFrame is empty
    print("Is the DataFrame empty?", df2.is_empty())
    
    # Output:
    # DataFrame after clear():
    # shape: (0, 4)
    ┌─────────┬──────┬──────────┬──────────┐
    │ Courses ┆ Fees ┆ Duration ┆ Discount │
    │ ---     ┆ ---  ┆ ---      ┆ ---      │
    │ str     ┆ i64  ┆ str      ┆ i64      │
    ╞═════════╪══════╪══════════╪══════════╡
    └─────────┴──────┴──────────┴──────────┘
    # Is the DataFrame empty? True
    

    Here,

    <a id="h-clear-a-dataframe-with-missing-values-nan"></a>

    Clear a DataFrame with Missing Values (NaN)

    You can use the clear(n) method to remove all rows while keeping the Polars DataFrame structure intact. If your DataFrame contains missing values (NaN or Nulls), clear() still removes all rows, regardless of whether they have missing values or not.

    
    import polars as pl
    
    technologies= {
        'Courses':["Spark","PySpark",None,"Python","Pandas"],
        'Fees' :[22000,None,23000,24000,26000],
        'Duration':['30days','50days','35days', None,'55days'],
        'Discount':[1000,2300,1000,1200,None]
              }
    df = pl.DataFrame(technologies)
    
    # Clearing the DataFrame (removes all rows)
    df2 = df.clear()
    print("DataFrame after clear():\n", df2)
    
    # Keeping only the first 2 rows
    df2 = df.clear(n=2)
    print("DataFrame after clear(n=2):\n", df2)
    
    # Output:
    # DataFrame after clear():
    # shape: (0, 4)
    ┌─────────┬──────┬──────────┬──────────┐
    │ Courses ┆ Fees ┆ Duration ┆ Discount │
    │ ---     ┆ ---  ┆ ---      ┆ ---      │
    │ str     ┆ i64  ┆ str      ┆ i64      │
    ╞═════════╪══════╪══════════╪══════════╡
    └─────────┴──────┴──────────┴──────────┘
    # DataFrame after clear(n=2):
    # shape: (2, 4)
    ┌─────────┬──────┬──────────┬──────────┐
    │ Courses ┆ Fees ┆ Duration ┆ Discount │
    │ ---     ┆ ---  ┆ ---      ┆ ---      │
    │ str     ┆ i64  ┆ str      ┆ i64      │
    ╞═════════╪══════╪══════════╪══════════╡
    │ null    ┆ null ┆ null     ┆ null     │
    │ null    ┆ null ┆ null     ┆ null     │
    └─────────┴──────┴──────────┴──────────┘
    

    Here,

    <a id="h-conclusion"></a>

    Conclusion

    In conclusion, the clear() method in Polars is a simple yet powerful function that removes all rows from a DataFrame while preserving its column names and data types. It is particularly useful when you need to reset a DataFrame without losing its schema.

    Happy Learning!!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References