
本文介绍了在 Polars 中对两个 LazyFrame 进行列级相乘的方法。由于 LazyFrame 不支持直接的乘法运算符,因此需要通过 join 操作和列选择来实现。文章提供了详细的代码示例,并解释了每个步骤的作用,帮助读者理解并应用该方法。
在 Polars 中,直接使用乘法运算符 * 对两个 LazyFrame 对象进行列级相乘是不支持的。尝试这样做会导致 TypeError 异常。为了实现这一目标,我们需要采用一种替代方案,该方案的核心思想是利用 join 操作将两个 LazyFrame 连接起来,然后选择相应的列进行乘法运算。
实现步骤
添加行索引: 首先,我们需要为两个 LazyFrame 添加行索引,以便后续的 join 操作能够正确匹配对应的行。可以使用 with_row_index() 方法来实现。
df1_with_index = df1.with_row_index() df2_with_index = df2.with_row_index()
执行 Join 操作: 使用 join 方法将两个带有行索引的 LazyFrame 连接起来。on="index" 参数指定了连接的键为 "index" 列。
joined_df = df1_with_index.join(df2_with_index, on="index")
选择并相乘列: 使用 select 方法选择需要相乘的列,并进行乘法运算。pl.col(col) * pl.col(f"{col}_right") 表达式将 df1 中的列 col 与 df2 中对应的列 col_right 相乘。
multiplied_df = joined_df.select(pl.col(col) * pl.col(f"{col}_right") for col in df1.columns)收集结果: 由于我们处理的是 LazyFrame,所以需要使用 collect() 方法将结果转换为 DataFrame,以便查看和使用。
result_df = multiplied_df.collect()
完整代码示例
import polars as pl
import numpy as np
# 创建示例 LazyFrame
n = 10
df1 = pl.DataFrame(data={
'foo': np.random.uniform(0,127, size= n).astype(np.float64),
'bar': np.random.uniform(1e3,32767, size= n).astype(np.float64),
'baz': np.random.uniform(1e6,2147483, size= n).astype(np.float64)
}).lazy()
df2 = pl.DataFrame(data={
'foo': np.random.uniform(0,127, size= n).astype(np.float64),
'bar': np.random.uniform(1e3,32767, size= n).astype(np.float64),
'baz': np.random.uniform(1e6,2147483, size= n).astype(np.float64)
}).lazy()
# 列级相乘
result_df = (
df1.with_row_index()
.join(df2.with_row_index(), on="index")
.select(pl.col(col) * pl.col(f"{col}_right") for col in df1.columns)
.collect()
)
print(result_df)代码解释
注意事项
总结
虽然 Polars 的 LazyFrame 不支持直接的列级相乘,但我们可以通过 join 操作和列选择来实现相同的功能。这种方法可以有效地处理大型数据集,并避免将整个数据集加载到内存中。通过理解并应用上述步骤,你可以轻松地在 Polars 中进行 LazyFrame 的列级相乘操作。
以上就是Polars LazyFrame 列级相乘的实现方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号