polars.Expr.list.gather#

Expr.list.gather(
indices: Expr | Series | list[int] | list[list[int]],
*,
null_on_oob: bool = False,
) Expr[source]#

通过多个索引获取子列表。

索引可以定义在单个列中,或者通过另一个列中的子列表定义,该列的数据类型为List

Parameters:
indices

每个子列表返回的索引

null_on_oob

如果索引超出范围的行为: True -> 设置为null False -> 抛出错误 请注意,默认抛出错误的成本要低得多

示例

>>> df = pl.DataFrame({"a": [[3, 2, 1], [], [1, 2, 3, 4, 5]]})
>>> df.with_columns(gather=pl.col("a").list.gather([0, 4], null_on_oob=True))
shape: (3, 2)
┌─────────────┬──────────────┐
│ a           ┆ gather       │
│ ---         ┆ ---          │
│ list[i64]   ┆ list[i64]    │
╞═════════════╪══════════════╡
│ [3, 2, 1]   ┆ [3, null]    │
│ []          ┆ [null, null] │
│ [1, 2, … 5] ┆ [1, 5]       │
└─────────────┴──────────────┘