![](/img/trans.png)
[英]how to apply .strip().split() function to an entire column in a Pandas dataframe
[英]strip entire polars dataframe
我想用這行代碼從極坐標 dataframe 中去除前導和尾隨空格:
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)]
)
但它沒有用。 我怎樣才能剝離整個極地 dataframe?
字符串列有相應的方法.strip()
( 請參閱文檔)
df.select(pl.all().str.strip())
此外,如果您的列具有不同的數據類型 - 您可以通過 select 僅字符串列pl.col(pl.Utf8)
:
df.select(pl.col(pl.Utf8).str.strip())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.