pyarrow将列添加到pyarrow表

问题描述

我有一个金字塔表名称final_table,形状为6132,7,我想在此表中添加列

 list_ = ['IT'] * 6132
 final_table.append_column('COUNTRY_ID',list_)

但是我收到以下错误ArrowInvalid:添加的列的长度必须与表的长度匹配。预期长度为6132,但长度为12264

解决方法

根据documentation

Append column at end of columns.

Parameters
field (str or Field) – If a string is passed then the type is deduced from the column data.

column (Array,list of Array,or values coercible to arrays) – Column data.

Returns
pyarrow.Table – New table with the passed column added.

我认为pyarrow假设您提供的是list of Array。为避免混淆,您应该改为传递箭头数组

col_a = pa.array([1,2,3],pa.int32())
col_b = pa.array(["X","Y","Z"],pa.string())

table = pa.Table.from_arrays(
    [col_a,col_b],schema=pa.schema([
        pa.field('a',col_a.type),pa.field('b',col_b.type),])
)

table.append_column('COUNTRY_ID',pa.array(['IT'] * len(table),pa.string()))

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...