1

I have a polars dataframe like this:

test=pl.DataFrame({"myColumn": [[1,2,3],[1,2,3],[1,2,3]]})

Now i would like to append list elements from another list, lets say [4,5] to each of the entries, so to get [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]]

Q1: How would that be done ? Q2: What would be an approach to make it fast ?

1 Answers1

3

Polars Series/columns of dtype List have a .list namespace. You can use the list.concat method to append a list.

df = pl.DataFrame({"my_column": [[1,2,3],[1,2,3],[1,2,3]]})
df.with_column(pl.col("my_column").list.concat([4, 5]))

The output is:

shape: (3, 1)
┌───────────────┐
│ my_column     │
│ ---           │
│ list [i64]    │
╞═══════════════╡
│ [1, 2, ... 5] │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ [1, 2, ... 5] │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ [1, 2, ... 5] │
└───────────────┘

ritchie46
  • 10,405
  • 1
  • 24
  • 43
  • Thanks, that works. I haven't run a benchmark, but i can already tell, that for my purpose it is fast enough. – randomSteph Jan 06 '22 at 18:57
  • Next release a faster solution will be available. Then I will update my answer. – ritchie46 Jan 07 '22 at 06:44
  • 1
    did you implement the faster solution? I'd be interested to know –  Aug 08 '22 at 16:41
  • 1
    Since polars 0.18.0 `.arr` is replaced by `.list`. Hence, `df.with_column(pl.col("my_column").arr.concat([4, 5]))` is now `df.with_columns(pl.col("my_column").list.concat([4, 5]))` – dikesh Jun 24 '23 at 11:47