You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何以最优方式展开GroupBy聚合后的Polars DataFrame?

问题:高效展开Polars DataFrame中的列表字段

给定如下Polars DataFrame,其中包含标量字段和列表类型字段:

import polars as pl
df = pl.DataFrame(
    [
        pl.Series("chromosome", ['chr1', 'chr1', 'chr1', 'chr1'], dtype=pl.Utf8),
        pl.Series("starts", [10, 1, 4, 7], dtype=pl.Int64),
        pl.Series("ends", [11, 4, 5, 8], dtype=pl.Int64),
        pl.Series("starts_right", [[6, 8], [0, 5], [5, 0], [6, 5]], dtype=pl.List(pl.Int64)),
        pl.Series("ends_right", [[10, 9], [2, 7], [7, 2], [10, 7]], dtype=pl.List(pl.Int64)),
    ]
)

其结构为:

shape: (4, 5)
┌────────────┬────────┬──────┬──────────────┬────────────┐
│ chromosome ┆ starts ┆ ends ┆ starts_right ┆ ends_right │
│ ---        ┆ ---    ┆ ---  ┆ ---          ┆ ---        │
│ str        ┆ i64    ┆ i64  ┆ list[i64]    ┆ list[i64]  │
╞════════════╪════════╪══════╪══════════════╪════════════╡
│ chr1       ┆ 10     ┆ 11   ┆ [6, 8]       ┆ [10, 9]    │
│ chr1       ┆ 1      ┆ 4    ┆ [0, 5]       ┆ [2, 7]     │
│ chr1       ┆ 4      ┆ 5    ┆ [5, 0]       ┆ [7, 2]     │
│ chr1       ┆ 7      ┆ 8    ┆ [6, 5]       ┆ [10, 7]    │
└────────────┴────────┴──────┴──────────────┴────────────┘

需要以最低开销展开该DataFrame,让标量字段重复对应次数,列表字段的每个元素单独成行,最终得到如下结构:

┌────────────┬────────┬──────┬──────────────┬────────────┐
│ chromosome ┆ starts ┆ ends ┆ starts_right ┆ ends_right │
│ ---        ┆ ---    ┆ ---  ┆ ---          ┆ ---        │
│ str        ┆ i64    ┆ i64  ┆ i64          ┆ i64        │
╞════════════╪════════╪══════╪══════════════╪════════════╡
│ chr1       ┆ 10     ┆ 11   ┆ 6            ┆ 10         │
│ chr1       ┆ 10     ┆ 11   ┆ 8            ┆ 9          │
│ chr1       ┆ 1      ┆ 4    ┆ 0            ┆ 2          │
│ chr1       ┆ 1      ┆ 4    ┆ 5            ┆ 7          │
...
解决方案

使用Polars内置的explode方法即可实现,该方法是矢量化操作,能以极低开销完成列表字段的展开,同时自动重复对应行的标量字段:

# 展开指定的列表字段
df_exploded = df.explode(["starts_right", "ends_right"])

# 或者如果DataFrame中所有列表字段都需要展开,可直接调用无参explode
# df_exploded = df.explode()

执行后得到的结果即为需求中的结构:

shape: (8, 5)
┌────────────┬────────┬──────┬──────────────┬────────────┐
│ chromosome ┆ starts ┆ ends ┆ starts_right ┆ ends_right │
│ ---        ┆ ---    ┆ ---  ┆ ---          ┆ ---        │
│ str        ┆ i64    ┆ i64  ┆ i64          ┆ i64        │
╞════════════╪════════╪══════╪══════════════╪════════════╡
│ chr1       ┆ 10     ┆ 11   ┆ 6            ┆ 10         │
│ chr1       ┆ 10     ┆ 11   ┆ 8            ┆ 9          │
│ chr1       ┆ 1      ┆ 4    ┆ 0            ┆ 2          │
│ chr1       ┆ 1      ┆ 4    ┆ 5            ┆ 7          │
│ chr1       ┆ 4      ┆ 5    ┆ 5            ┆ 7          │
│ chr1       ┆ 4      ┆ 5    ┆ 0            ┆ 2          │
│ chr1       ┆ 7      ┆ 8    ┆ 6            ┆ 10         │
│ chr1       ┆ 7      ┆ 8    ┆ 5            ┆ 7          │
└────────────┴────────┴──────┴──────────────┴────────────┘

内容的提问来源于stack exchange,提问作者The Unfun Cat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 20:24:53