Search code examples
pythonpython-polars

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)

Solution

  • 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             │
    └─────────────────┘