python 如何将Pandas过滤器转换为Polars过滤器?

k4ymrczo  于 11个月前  发布在  Python
关注(0)|答案(1)|浏览(113)

我想把python pandas filter转换成python polars filter。

  1. df.loc[:,df.dtypes == object] = df.loc[:,df.dtypes == object].applymap(lambda x:str(x).strip())
  2. df.loc[:,df.dtypes == object] = df.loc[:,df.dtypes == object].replace('nan',np.NaN)
  3. df[dateCols 'fileColumn ']] = pd.to_datetime(df[dateCols 'fileColumn']],format=dtpattern,errors='raise')
    有人能把这三个pandas过滤器转换成polars吗?把pandas过滤器转换成polars有点棘手。
xzv2uavs

xzv2uavs1#

我将做我认为你正在尝试做的事情(但我同意你应该发布一个可复制的示例的评论)
1.

import polars.selectors as cs
df = df.with_columns(cs.string().str.strip_chars())

字符串

import polars.selectors as cs
df.with_columns(pl.when(cs.string() != 'nan').then(cs.string()).otherwise(pl.lit(None)))

df = df.with_columns(pl.col('fileColumn').str.to_datetime(format=dtpattern))

相关问题