将类型为list[]的列转换为字符串在polars中

huangapple go评论85阅读模式
英文:

Cast column of type list[] to str in polars

问题

当前,在polars的列类型为list[]上使用cast()方法是不受支持的。它会抛出以下错误:

ComputeError: 无法将列表类型转换

在我通常所做的之前(使用rows(),或转换为pandas,或使用apply()方法),是否有任何技巧或最佳实践来将polars的list[]转换为字符串?

以下是一个快速的示例代码,供您复制错误:

df = pl.from_dict({'foo': [[1,2,3]], 'bar': 'Hello World'})
print(df)

我期望看到的输出是:

shape: (1, 2)
┌───────────┬─────────────┐
 foo        bar         
 ---        ---         
 list[i64]  str         
╞═══════════╪═════════════╡
 [1, 2, 3]  Hello World 
└───────────┴─────────────┘

这是我期望看到的输出。

英文:

Currently, using the polars' cast() method on columns of type list[] is not supported. It throws:

ComputeError: Cannot cast list type

Before I do as usual (use rows(), or convert to pandas, or work with apply()). Is there any trick or best practice to convert polars list[] to strings?

Here a quick snippet for you to replicate the error

df = pl.from_dict({'foo': [[1,2,3]], 'bar': 'Hello World'})
print(df)
'''
shape: (1, 2)
┌───────────┬─────────────┐
│ foo       ┆ bar         │
│ ---       ┆ ---         │
│ list[i64] ┆ str         │
╞═══════════╪═════════════╡
│ [1, 2, 3] ┆ Hello World │
└───────────┴─────────────┘
'''
df['foo'].cast(str)
# this other workaround wont work neither
df.select([pl.col('foo').str])

Here is what i expect to see:

'''
shape: (1, 2)
┌───────────┬─────────────┐
│ foo       ┆ bar         │
│ ---       ┆ ---         │
│ str       ┆ str         │
╞═══════════╪═════════════╡
│"[1, 2, 3]"┆ Hello World │
└───────────┴─────────────┘
'''

答案1

得分: 2

你可以使用数据类型 pl.List(pl.Utf8)

df.with_columns(pl.col("foo").cast(pl.List(pl.Utf8)))

创建实际字符串的示例:

df.with_columns("[" + 
   pl.col("foo").cast(pl.List(pl.Utf8)).list.join(", ") 
   + "]"
)

或者

df.with_columns(
   pl.format("[{}]",
      pl.col("foo").cast(pl.List(pl.Utf8)).list.join(", ")))
英文:

You can use the datatype pl.List(pl.Utf8)

df.with_columns(pl.col("foo").cast(pl.List(pl.Utf8)))
shape: (1, 2)
┌─────────────────┬─────────────┐
│ foo             | bar         │
│ ---             | ---         │
│ list[str]       | str         │
╞═════════════════╪═════════════╡
│ ["1", "2", "3"] | Hello World │
└─────────────────┴─────────────┘

To create an actual string - perhaps:

df.with_columns("[" + 
   pl.col("foo").cast(pl.List(pl.Utf8)).list.join(", ") 
   + "]"
)

or

df.with_columns(
   pl.format("[{}]",
      pl.col("foo").cast(pl.List(pl.Utf8)).list.join(", ")))
shape: (1, 3)
┌───────────┬─────────────┬───────────┐
│ foo       | bar         | literal   │
│ ---       | ---         | ---       │
│ list[i64] | str         | str       │
╞═══════════╪═════════════╪═══════════╡
│ [1, 2, 3] | Hello World | [1, 2, 3] │
└───────────┴─────────────┴───────────┘

huangapple
  • 本文由 发表于 2023年3月3日 22:36:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定