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

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

Cast column of type list[] to str in polars

问题

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

  1. ComputeError: 无法将列表类型转换

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

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

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

我期望看到的输出是:

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

这是我期望看到的输出。

英文:

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

  1. 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

  1. df = pl.from_dict({'foo': [[1,2,3]], 'bar': 'Hello World'})
  2. print(df)
  3. '''
  4. shape: (1, 2)
  5. ┌───────────┬─────────────┐
  6. foo bar
  7. --- ---
  8. list[i64] str
  9. ╞═══════════╪═════════════╡
  10. [1, 2, 3] Hello World
  11. └───────────┴─────────────┘
  12. '''
  13. df['foo'].cast(str)
  14. # this other workaround wont work neither
  15. df.select([pl.col('foo').str])

Here is what i expect to see:

  1. '''
  2. shape: (1, 2)
  3. ┌───────────┬─────────────┐
  4. foo bar
  5. --- ---
  6. str str
  7. ╞═══════════╪═════════════╡
  8. │"[1, 2, 3]"┆ Hello World
  9. └───────────┴─────────────┘
  10. '''

答案1

得分: 2

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

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

创建实际字符串的示例:

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

或者

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

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

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

To create an actual string - perhaps:

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

or

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

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:

确定