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

如何用Polars原生函数将DataFrame的list[String]列转为字符串列?

使用Polars原生函数拼接list[String]列为字符串列

直接用Polars内置的list.join方法就能搞定,完全不需要自定义Python函数,代码如下:

import polars as pl

df = pl.DataFrame({
    'col1': [["1.0", "1.0"], ["2.0", "3.0"]],
    'col2': ["a", "a"],
})

# 对list[String]类型的col1列进行拼接
result_df = df.with_columns(
    pl.col('col1').list.join(',')
)

print(result_df)

执行后得到的结果和预期一致:

shape: (2, 2)
┌─────────┬──────┐
│ col1    ┆ col2 │
│ ---     ┆ ---  │
│ str     ┆ str  │
╞═════════╪══════╡
│ 1.0,1.0 ┆ a    │
│ 2.0,3.0 ┆ a    │
└─────────┴──────┘

核心逻辑就是list.join(delimiter)方法,它会把列表中的每个字符串元素用指定的分隔符(这里是逗号)拼接成单个字符串,完全是Polars原生实现,满足需求。

内容的提问来源于stack exchange,提问作者Dariusz Krynicki

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 20:12:37