相关疑难解决方法(0)

将函数应用于 Polars-DataFrame 的所有列

我知道如何将函数应用于Pandas-DataFrame中存在的所有列。但是,我还没有弄清楚如何在使用Polars-DataFrame时实现这一点。

我查看了《Polars 用户指南》中专门讨论此主题的部分,但我还没有找到答案。在这里,我附上了我不成功的尝试的代码片段。

import numpy as np
import polars as pl
import seaborn as sns

# Loading toy dataset as Pandas DataFrame using Seaborn
df_pd = sns.load_dataset('iris')

# Converting Pandas DataFrame to Polars DataFrame
df_pl = pl.DataFrame(df_pd)

# Dropping the non-numeric column...
df_pd = df_pd.drop(columns='species')                     # ... using Pandas
df_pl = df_pl.drop('species')                             # ... using Polars

# Applying function to the whole DataFrame...
df_pd_new = df_pd.apply(np.log2)                          # ... using Pandas
# df_pl_new …
Run Code Online (Sandbox Code Playgroud)

python apply dataframe pandas python-polars

9
推荐指数
1
解决办法
9929
查看次数

标签 统计

apply ×1

dataframe ×1

pandas ×1

python ×1

python-polars ×1