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 –
- Polars string concatenation combines values from two or more string columns into a single column.
- String concatenation joins values from two columns row-wise into a single string.
- The
+operator can be used for simple concatenation without any separator. - The
pl.concat_str()function enables concatenation with custom separators like spaces, commas, or other characters. - Static text or literals can be added between or around columns using
pl.lit(). - Both columns involved in concatenation must be of string type; non-string columns should be cast.
<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,
pl.concat_str()combines string columns.- No
separator= no space, comma, or symbol between the strings. - Use
alias()to name the new column.
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,
- Use
pl.concat_str()to concatenate string columns. separator=" "adds a space between the values.alias()sets the name of the new concatenated column
<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,
- Use
separator=", "to insert a comma + space between values. - Makes the concatenated string more readable.
alias()names the new column.
<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,
pl.lit("text")creates a literal string expression.pl.col("ColumnName")refers to the column values.pl.concat_str([...])concatenates all parts in order.
<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
- Polars DataFrame Drop Nans
- Add Row of Column Totals in Polars
- Polars Delete a Column from DataFrame
- Polars String Manipulation of Cell Contents
- Polars Replace String in Multiple Columns
- Polars DataFrame Columns Selection
- Polars Adding Days to a Date
- How to use isin in Polars DataFrame?
- Retrieve Date from DateTime Column in Polars
- How to Effectively Create Duplicate Rows in Polars?
- Polars DataFrame with_columns() Function with Examples
- How to Get the Name of an Aliased Expression in Polars?
<a id="h-references"></a>
