Horseman
Horseman

Reputation: 417

strip entire polars dataframe

I want to strip leading and trailing whitespaces from a polars dataframe with this line of code:

df = pl.DataFrame(
    {
        "A": ["foo ", "ham", "spam ", "egg",],
        "L": ["A54", " A12", "B84", " C12"],

    }
)
print(df)

shape: (4, 2)
┌───────┬──────┐
│ A     ┆ L    │
│ ---   ┆ ---  │
│ str   ┆ str  │
╞═══════╪══════╡
│ foo   ┆ A54  │
│ ham   ┆  A12 │
│ spam  ┆ B84  │
│ egg   ┆  C12 │
└───────┴──────┘

df_clean = df.select(
            [pl.all().map(lambda x: x.strip() if isinstance(x, str) else x)]
        )

But it did not work. How can I strip an entire polars dataframe?

Upvotes: 2

Views: 3508

Answers (2)

Cristian Elizalde
Cristian Elizalde

Reputation: 66

.str.strip() is deprecated now.

.str.strip_chars() should be used instead.

Upvotes: 5

glebcom
glebcom

Reputation: 1462

There is corresponding method .strip() for string columns (see doc)

df.select(pl.all().str.strip())

Also if you have columns with different data types - you can select only string columns by pl.col(pl.Utf8):

df.select(pl.col(pl.Utf8).str.strip())

Upvotes: 5

Related Questions