Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With Polars, how to concatenate list-of-string expression/column to string

Here's a naive solution of what I want to do using map_elements. How can I do this with only Polars functions?

import polars as pl

# Create a DataFrame with a column containing lists of strings
df = pl.DataFrame({
    "list_of_strings": [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]
})

# Define a function to concatenate lists of strings into a single string
def concatenate_list_of_strings(lst):
    return "".join(lst)

# Apply the function to the DataFrame
df = df.with_column(
    pl.col("list_of_strings").map_elements(concatenate_list_of_strings, return_dtype=pl.String).alias("concatenated_string")
)

print(df)
like image 372
HumpbackWhale194 Avatar asked Feb 03 '26 15:02

HumpbackWhale194


1 Answers

As already mentioned in the comments, there is pl.Expr.list.join in polars' native expression API to join all string items in a sublist with a separator between them.

df.with_columns(
    pl.col("list_of_strings").list.join("")
)
shape: (3, 1)
┌─────────────────┐
│ list_of_strings │
│ ---             │
│ str             │
╞═════════════════╡
│ abc             │
│ def             │
│ ghi             │
└─────────────────┘
like image 67
Hericks Avatar answered Feb 06 '26 05:02

Hericks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!