寻求在 Polars DataFrame 的所有列上应用字符串替换,而无需指定每一列。

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

Looking to apply a string replace for all columns of a Polars DataFrame without specifying each column

问题

I'm trying to apply a string replace to a Polars DataFrame, similar to how you would apply it to a Pandas DataFrame.

我正在尝试对Polars DataFrame应用字符串替换,类似于您在Pandas DataFrame上的操作。

I would like the equivalent to the following:

我想要等效于以下内容:

df = df.apply(lambda x: x.str.replace("A", "B")

df = df.apply(lambda x: x.str.replace("A", "B")

I understand with Polars you can use the following:

我了解在Polars中,您可以使用以下方法:

df = df.with_columns(
    pl.col("col1").str.replace("A", "B"),
    pl.col("col2").str.replace("A", "B"))
df = df.with_columns(
    pl.col("col1").str.replace("A", "B"),
    pl.col("col2").str.replace("A", "B"))

Is there a way to apply it to all columns? I have a very large DataFrame (100+ columns) and would prefer to not have to specify all columns.

是否有一种方法可以将其应用于所有列?我有一个非常大的DataFrame(100多列),并且不想指定所有列。

I've tried using:

我尝试过使用:

df = df.select([
    pl.all().map(lambda x: x.replace("A", "B"), df)
])
df = df.select([
    pl.all().map(lambda x: x.replace("A", "B"), df)
])

and I get an error: ValueError: Cannot infer dtype from 'shape: (150000, 150)

并且我收到一个错误:ValueError: Cannot infer dtype from 'shape: (150000, 150)

Is there something that I may be missing?

我可能漏掉了什么吗?

英文:

I'm trying to apply a string replace to a Polars DataFrame, similar to how you would apply it to a Pandas DataFrame.

I would like the equivalent to the following:

df = df.apply(lambda x: x.str.replace("A", "B")

I understand with Polars you can use the following:

`
df=df.with_columns(
    pl.col("col1").str.replace("A", "B"),
    pl.col("col2").str.replace("A", "B"))`

Is there a way to apply it to all columns? I have a very large DataFrame (100+ columns) and would prefer to not have to specify all columns.

I've tried using:

df = df.select([
    pl.all().map(lambda x: x.replace("A", "B"), df)
])

and I get an error: ValueError: Cannot infer dtype from 'shape: (150000, 150)

Is there something that I may be missing?

答案1

得分: 1

df=df.with_columns(pl.col(pl.Utf8).str.replace("A", "B"))

pl.all().str ... 如果数据框完全是字符串列,也可以。

英文:

You can select all columns of a certain type, or multiple columns. See Selectors for more.

df=df.with_columns(pl.col(pl.Utf8).str.replace("A", "B"))

pl.all().str ... is fine too if the df is fully string columns.

答案2

得分: 0

尝试:

df = df.with_columns([
    pl.all().str.replace("A", "B")
])
英文:

Instead of your initial solution:

df = df.select([
    pl.all().map(lambda x: x.replace("A", "B"), df)
])

Try:

df = df.with_columns([
    pl.all().str.replace("A", "B")
])

huangapple
  • 本文由 发表于 2023年6月22日 06:38:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527571.html
匿名

发表评论

匿名网友

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

确定