如何将 python 列表附加到 polars-dataframe 的另一个列表(系列)?

How to append a python list to another list (Series) of a polars-dataframe?

我有一个像这样的极坐标数据框:

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

现在我想从另一个列表中追加列表元素,让我们对每个条目说 [4,5],以便得到 [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]]

Q1:那要怎么做? Q2:有什么方法可以让它变快?

dtype List 的极坐标 Series/columns 有一个 .arr(用于数组)命名空间。您可以使用 arr.concat 方法附加列表。

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

输出为:

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