Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

I would like the equivalent to the following:

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading