Joining text data with polars

100DaysOfPolars
Author

Joram Mutenge

Published

2025-07-01

Most people have a love-hate relationship with text data, but if you work in the data field, you’re bound to encounter it.

One of the most common operations when working with text data is joining two words. Below is a dataframe containing the first and last names of characters from one of my favorite TV shows, Suits.

shape: (4, 2)
First_Name Last_Name
str str
"Harvey" "Specter"
"Mike" "Ross"
"Jessica" "Pearson"
"Louis" "Litt"


Let’s say we want to create a new column called Full_Name that combines the first and last names into one. How would we do that?

This is where the Polars expression concat_str comes in handy. Here’s how to use it:

(df
 .with_columns(pl.concat_str([pl.col('First_Name'), pl.col('Last_Name')], separator=' ')
               .alias('Full_Name'))
 )
shape: (4, 3)
First_Name Last_Name Full_Name
str str str
"Harvey" "Specter" "Harvey Specter"
"Mike" "Ross" "Mike Ross"
"Jessica" "Pearson" "Jessica Pearson"
"Louis" "Litt" "Louis Litt"


The code above is considered the idiomatic Polars way of doing it.

But if you want to impress your boss with your technical wizardry, you can also achieve the same result using a more “hacky” approach like this:

(df
 .with_columns(pl.col('First_Name').add(' ').add(pl.col('Last_Name'))
               .alias('Full_Name')
               )
 )
shape: (4, 3)
First_Name Last_Name Full_Name
str str str
"Harvey" "Specter" "Harvey Specter"
"Mike" "Ross" "Mike Ross"
"Jessica" "Pearson" "Jessica Pearson"
"Louis" "Litt" "Louis Litt"


Become a master of Polars by enrolling in the Polars course.