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

如何向PyArrow表中数组的每个元素追加字符串?

在PyArrow中给字符串列元素追加固定后缀

现有代码:

import pyarrow as pa

t = pa.table({'a': ['one', 'two', 'three']})

需要给列a的每个字符串末尾加上_frobenius,最终得到这样的结果:

pyarrow.Table
a: string
----
a: [["one_frobenius","two_frobenius","three_frobenius"]]

直接用PyArrow的计算函数就能搞定,两种写法都可以:

方法一:用concat拼接数组

import pyarrow as pa
import pyarrow.compute as pc

t = pa.table({'a': ['one', 'two', 'three']})
# 生成和原列长度一致的后缀数组
suffix = pa.repeat('_frobenius', len(t))
# 把原列和后缀数组拼接
new_a_col = pc.concat([t['a'], suffix])
# 替换原表的a列
new_table = t.set_column(0, 'a', new_a_col)

print(new_table)

方法二:用binary_add直接拼接字符串

PyArrow里字符串类型支持binary_add操作,写法更简洁:

import pyarrow as pa
import pyarrow.compute as pc

t = pa.table({'a': ['one', 'two', 'three']})
new_table = t.set_column(0, 'a', pc.binary_add(t['a'], '_frobenius'))

print(new_table)

运行任意一种写法,都能得到你想要的输出。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 00:39:53