Built with Tutify.io

    AIStudyLabs

    Polars String Concatenation of Two Existing Columns

    Polars String Concatenation of Two Existing Columns

    In Polars, you can concatenate two string columns either with the + operator or by using the pl.concat_str() function, which provides additional options like adding separators. String concatenation in Polars involves combining the text from two separate columns on a row-by-row basis to produce a new column containing the joined strings. In this article, I will explain the polars string concatenation of two existing columns.

    Key Points –

    <a id="h-usage-of-polars-string-concatenation-of-two-existing-columns"></a>

    Usage of Polars String Concatenation of Two Existing Columns

    String concatenation in Polars refers to joining the values of two or more string columns into one, on a row-by-row basis. It’s useful for merging text fields, like first and last names, adding prefixes or suffixes, or creating formatted strings.

    Now, let’s create a Polars DataFrame.

    
    import polars as pl
    
    df = pl.DataFrame({
        'Courses':["Spark","PySpark","Python","Pandas"],
        'Duration':['30days','40days','60days','50days']
    })
    print("Original DataFrame:\n", df)
    

    Yields below output.

    To concatenate two existing columns in Polars without any separator, you can use the + operator. For example, to combine the Courses and Duration columns in your Polars DataFrame without a separator, simply create a new column by directly adding these two string columns together.

    
    # Simple concatenation without separator
    df2 = df.with_columns(
        (pl.col("Courses") + pl.col("Duration")).alias("CourseInfo")
    )
    print(df2)
    

    Yields below output.

    <a id="h-use-concatenation-no-separator"></a>

    Use Concatenation (No Separator)

    Alternatively, to perform a simple concatenation with no separator in Polars (i.e., join values from multiple columns together without any space or character between), you can use the pl.concat_str() function.

    
    # Simple concatenation of 'Courses' and 'Duration' without a separator
    df = df.with_columns(
        pl.concat_str(["Courses", "Duration"]).alias("CourseInfo")
    )
    print(df)
    

    Here,

    Yields the same output as above.

    <a id="h-concatenation-with-space-separator"></a>

    Concatenation with Space Separator

    To concatenate two columns with a space between them in Polars, use pl.concat_str() and set separator=" ". This adds a space separator when joining the columns.

    
    # Concatenate with space separator
    df2 = df.with_columns(
        pl.concat_str(["Courses", "Duration"], separator=" ").alias("CourseInfo")
    )
    print(df2)
    
    # Output:
    # shape: (4, 3)
    ┌─────────┬──────────┬────────────────┐
    │ Courses ┆ Duration ┆ CourseInfo     │
    │ ---     ┆ ---      ┆ ---            │
    │ str     ┆ str      ┆ str            │
    ╞═════════╪══════════╪════════════════╡
    │ Spark   ┆ 30days   ┆ Spark 30days   │
    │ PySpark ┆ 40days   ┆ PySpark 40days │
    │ Python  ┆ 60days   ┆ Python 60days  │
    │ Pandas  ┆ 50days   ┆ Pandas 50days  │
    └─────────┴──────────┴────────────────┘
    

    Here,

    <a id="h-concatenation-with-comma-separator"></a>

    Concatenation with Comma Separator

    To join two columns using a comma and space as the separator, simply set separator=", " in pl.concat_str(). This concatenates the columns with a comma and space between them in Polars.

    
    # Concatenate with comma and space separator
    df2 = df.with_columns(
        pl.concat_str(["Courses", "Duration"], separator=", ").alias("CourseInfo")
    )
    
    print(df2)
    
    # Concatenate with comma and space separator
    df2 = df.with_columns(
        (pl.col('Courses') + ", " + pl.col('Duration')).alias('CourseInfo')
    )
    print(df2)
    
    # Output:
    # shape: (4, 3)
    ┌─────────┬──────────┬─────────────────┐
    │ Courses ┆ Duration ┆ CourseInfo      │
    │ ---     ┆ ---      ┆ ---             │
    │ str     ┆ str      ┆ str             │
    ╞═════════╪══════════╪═════════════════╡
    │ Spark   ┆ 30days   ┆ Spark, 30days   │
    │ PySpark ┆ 40days   ┆ PySpark, 40days │
    │ Python  ┆ 60days   ┆ Python, 60days  │
    │ Pandas  ┆ 50days   ┆ Pandas, 50days  │
    └─────────┴──────────┴─────────────────┘
    

    Here,

    <a id="h-concatenating-with-custom-text"></a>

    Concatenating with Custom Text

    You can concatenate columns with custom text or strings inserted between or around them by using pl.concat_str() together with pl.lit() to add fixed strings. This allows you to combine two columns in Polars while including custom text as separators or wrappers.

    
    # Concatenate with custom text
    df2 = df.with_columns(
        (pl.lit("Course: ") + pl.col("Courses") + pl.lit(" | Duration: ") + pl.col("Duration")).alias("CourseInfo")
    )
    print(df2)
    
    # Output:
    # shape: (4, 3)
    ┌─────────┬──────────┬─────────────────────────────────┐
    │ Courses ┆ Duration ┆ CourseInfo                      │
    │ ---     ┆ ---      ┆ ---                             │
    │ str     ┆ str      ┆ str                             │
    ╞═════════╪══════════╪═════════════════════════════════╡
    │ Spark   ┆ 30days   ┆ Course: Spark | Duration: 30da… │
    │ PySpark ┆ 40days   ┆ Course: PySpark | Duration: 40… │
    │ Python  ┆ 60days   ┆ Course: Python | Duration: 60d… │
    │ Pandas  ┆ 50days   ┆ Course: Pandas | Duration: 50d… │
    └─────────┴──────────┴─────────────────────────────────┘
    

    Here,

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

    Conclusion

    In conclusion, Polars offers flexible and efficient ways to concatenate string columns, whether you want simple joining without separators, add spaces or commas, or insert custom text. Using operators like +, functions like pl.concat_str(), and literals with pl.lit(), you can easily create new combined columns tailored to your data needs.

    Happy Learning!!

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

    Related Articles

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

    References