Built with Tutify.io

    AIStudyLabs

    Polars DataFrame pipe() Usage & Examples

    Polars DataFrame pipe() Usage & Examples

    In Polars, the pipe() method follows a functional programming approach, enabling you to apply custom transformations to a DataFrame in a clear and modular manner. The key concept is to "pipe" the DataFrame into a function, perform the transformation, and return the updated DataFrame (or any other result). By using pipe(), you can seamlessly chain multiple transformations or operations into one expression, making your code more concise, readable, and maintainable.

    In this article, I will explain the pipe() method of the Polars DataFrame, covering its syntax, parameters, and practical use cases. This method returns the result of the function you provide, typically a new Polars DataFrame with the transformation applied.

    Key Points –

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

    Polars DataFrame pipe() Introduction

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

    
    # Syntax of Dataframe pipe()
    DataFrame.pipe(
    function: Callable[Concatenate[DataFrame, P], T],
    *args: P.args,
    **kwargs: P.kwargs,
    ) → T
    

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

    Parameters of the Polars DataFrame pipe()

    Following are the parameters of the pipe() method.

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

    Usage of Polars DataFrame pipe() Method

    The pipe() method in Polars allows you to chain custom transformations on a DataFrame in a clean, readable way, especially when you want to apply custom functions or multiple steps in a pipeline style.

    To run some examples of the Polars DataFrame pipe() function, 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','70days', '40days','55days'],
        'Discount':[1000,2300,2000,1200,2500]
              }
    df = pl.DataFrame(technologies)
    print("Original DataFrame:\n", df)
    

    Yields below output.

    To use the pipe() method to add a derived column in Polars, you can chain a transformation that computes a new column based on existing ones. For instance, you might want to calculate a new column based on the Fees and Discount columns.

    
    # Using pipe() to add a derived column (Final_Fee)
    df2 = df.pipe(
        lambda df: df.with_columns(
            (pl.col("Fees") - pl.col("Discount")).alias("Final_Fee")
        )
    )
    print("DataFrame with Derived Column:\n", df2)
    

    Here,

    <a id="h-using-pipe-method-to-apply-multiple-transformations"></a>

    Using pipe() Method to Apply Multiple Transformations

    If you want to apply multiple transformations on a Polars DataFrame using the pipe() method, it's a clean and elegant way to chain operations, especially useful when you want to modularize your transformations.

    
    # Transformation 1: Convert 'Duration' to integer
    def convert_duration(df):
        return df.with_columns(
            pl.col("Duration").str.extract(r"(\d+)", 1).cast(pl.Int32).alias("Duration_days")
        )
    
    # Transformation 2: Add a new column for final fee
    def add_final_fee(df):
        return df.with_columns(
            (pl.col("Fees") - pl.col("Discount")).alias("Final_Fee")
        )
    
    # Apply using pipe
    df_transformed = (df.pipe(convert_duration).pipe(add_final_fee))
    print("Transformed DataFrame:\n", df_transformed)
    
    # Output:
    # Transformed DataFrame:
    # shape: (5, 6)
    ┌─────────┬───────┬──────────┬──────────┬───────────────┬───────────┐
    │ Courses ┆ Fees  ┆ Duration ┆ Discount ┆ Duration_days ┆ Final_Fee │
    │ ---     ┆ ---   ┆ ---      ┆ ---      ┆ ---           ┆ ---       │
    │ str     ┆ i64   ┆ str      ┆ i64      ┆ i32           ┆ i64       │
    ╞═════════╪═══════╪══════════╪══════════╪═══════════════╪═══════════╡
    │ Spark   ┆ 22000 ┆ 30days   ┆ 1000     ┆ 30            ┆ 21000     │
    │ PySpark ┆ 25000 ┆ 50days   ┆ 2300     ┆ 50            ┆ 22700     │
    │ Hadoop  ┆ 23000 ┆ 70days   ┆ 2000     ┆ 70            ┆ 21000     │
    │ Python  ┆ 24000 ┆ 40days   ┆ 1200     ┆ 40            ┆ 22800     │
    │ Pandas  ┆ 26000 ┆ 55days   ┆ 2500     ┆ 55            ┆ 23500     │
    └─────────┴───────┴──────────┴──────────┴───────────────┴───────────┘
    

    <a id="h-rename-columns-using-pipe-method"></a>

    Rename Columns Using pipe() Method

    To rename the columns of a Polars DataFrame using the pipe() method, you can define a function that handles the renaming and then chain it through the pipe() function.

    
    # Renaming columns using pipe() method
    df2= df.pipe(lambda df: df.rename({"Courses": "Course_Name", "Fees": "Course_Fee", "Duration": "Course_Duration", "Discount": "Course_Discount"}))
    print("Renamed DataFrame:\n", df2)
    
    # Output:
    # Renamed DataFrame:
    # shape: (5, 4)
    ┌─────────────┬────────────┬─────────────────┬─────────────────┐
    │ Course_Name ┆ Course_Fee ┆ Course_Duration ┆ Course_Discount │
    │ ---         ┆ ---        ┆ ---             ┆ ---             │
    │ str         ┆ i64        ┆ str             ┆ i64             │
    ╞═════════════╪════════════╪═════════════════╪═════════════════╡
    │ Spark       ┆ 22000      ┆ 30days          ┆ 1000            │
    │ PySpark     ┆ 25000      ┆ 50days          ┆ 2300            │
    │ Hadoop      ┆ 23000      ┆ 70days          ┆ 2000            │
    │ Python      ┆ 24000      ┆ 40days          ┆ 1200            │
    │ Pandas      ┆ 26000      ┆ 55days          ┆ 2500            │
    └─────────────┴────────────┴─────────────────┴─────────────────┘
    

    Here,

    <a id="h-filter-rows-using-a-function-with-the-pipe"></a>

    Filter Rows Using a Function with the pipe()

    To filter rows using a function with the pipe() method in Polars, you can pass a lambda function inside pipe() that applies a condition to filter the rows based on one or more columns.

    Here’s an example where we use the pipe() method to filter the rows based on the Fees column (e.g., only courses where the fee is greater than 24000).

    
    # Using pipe() to filter rows where Fees > 24000
    df2 = df.pipe(
        lambda df: df.filter(pl.col("Fees") > 24000)
    )
    print("Filtered DataFrame:\n", df2)
    
    # Output:
    # Filtered DataFrame:
    # shape: (2, 4)
    ┌─────────┬───────┬──────────┬──────────┐
    │ Courses ┆ Fees  ┆ Duration ┆ Discount │
    │ ---     ┆ ---   ┆ ---      ┆ ---      │
    │ str     ┆ i64   ┆ str      ┆ i64      │
    ╞═════════╪═══════╪══════════╪══════════╡
    │ PySpark ┆ 25000 ┆ 50days   ┆ 2300     │
    │ Pandas  ┆ 26000 ┆ 55days   ┆ 2500     │
    └─────────┴───────┴──────────┴──────────┘
    

    Here,

    <a id="h-convert-column-data-types-using-pipe-method"></a>

    Convert Column Data Types Using pipe() Method

    You can convert column data types using the pipe() method in Polars by combining it with the with_columns() method and cast() function.

    Here’s a clear example of how to do it using pipe() to convert the Fees column from Int64 to Float64.

    
    # Convert 'Fees' column from Int64 to Float64 using pipe()
    df2 = df.pipe(
        lambda df: df.with_columns(
            pl.col("Fees").cast(pl.Float64)
        )
    )
    print("DataFrame with Converted Data Type:\n", df2)
    
    # Output:
    # DataFrame with Converted Data Type:
    # shape: (5, 4)
    ┌─────────┬─────────┬──────────┬──────────┐
    │ Courses ┆ Fees    ┆ Duration ┆ Discount │
    │ ---     ┆ ---     ┆ ---      ┆ ---      │
    │ str     ┆ f64     ┆ str      ┆ i64      │
    ╞═════════╪═════════╪══════════╪══════════╡
    │ Spark   ┆ 22000.0 ┆ 30days   ┆ 1000     │
    │ PySpark ┆ 25000.0 ┆ 50days   ┆ 2300     │
    │ Hadoop  ┆ 23000.0 ┆ 70days   ┆ 2000     │
    │ Python  ┆ 24000.0 ┆ 40days   ┆ 1200     │
    │ Pandas  ┆ 26000.0 ┆ 55days   ┆ 2500     │
    └─────────┴─────────┴──────────┴──────────┘
    

    Here,

    <a id="h-add-a-flag-based-on-condition-using-pipe-method"></a>

    Add a Flag Based on Condition Using pipe() Method

    To add a flag column based on a condition using the pipe() method in Polars.

    
    # Add a flag based on condition using pipi() method
    def add_long_duration_flag(df: pl.DataFrame) -> pl.DataFrame:
        return df.with_columns(
            (pl.col("Duration").str.strip_chars("days").cast(pl.Int32) > 50)
            .alias("Long_Duration")
        )
    
    df2 = df.pipe(add_long_duration_flag)
    print("DataFrame with Duration Flag:\n", df2)
    
    # Output:
    # DataFrame with Duration Flag:
    # shape: (5, 5)
    ┌─────────┬───────┬──────────┬──────────┬───────────────┐
    │ Courses ┆ Fees  ┆ Duration ┆ Discount ┆ Long_Duration │
    │ ---     ┆ ---   ┆ ---      ┆ ---      ┆ ---           │
    │ str     ┆ i64   ┆ str      ┆ i64      ┆ bool          │
    ╞═════════╪═══════╪══════════╪══════════╪═══════════════╡
    │ Spark   ┆ 22000 ┆ 30days   ┆ 1000     ┆ false         │
    │ PySpark ┆ 25000 ┆ 50days   ┆ 2300     ┆ false         │
    │ Hadoop  ┆ 23000 ┆ 70days   ┆ 2000     ┆ true          │
    │ Python  ┆ 24000 ┆ 40days   ┆ 1200     ┆ false         │
    │ Pandas  ┆ 26000 ┆ 55days   ┆ 2500     ┆ true          │
    └─────────┴───────┴──────────┴──────────┴───────────────┘
    

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

    Conclusion

    In summary, the pipe() method in Polars provides a powerful and readable way to apply multiple transformations. By encapsulating each transformation in a function and chaining them with pipe(), you keep your code modular, clean, and easy to maintain.

    Happy Learning!!

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

    Related Articles

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

    References