Pyspark 矩阵变换

数据挖掘 机器学习 Python 数据 预处理 pyspark
2022-03-13 17:50:15

假设我在 PySpark 中有以下数据框:

Customer    |  product  |   rating
customer1   |  product1 |   0.2343
customer1   |  product2 |   0.4440
customer2   |  product3 |   0.3123
customer3   |  product1 |   0.7430

可以有多个客户产品组合,但每个组合都是独一无二的。我想以最有效的方式归档以下结果:

Customer (Index) | product 1 | product 2 | product 3
customer 1       |   0.2343  |  0.4440   |  0.0000
customer 2       |   0.0000  |  0.0000   |  0.3123
customer 3       |   0.7430  |  0.0000   |  0.0000

第一个表中未表示的每个组合都将设置为零。它必须高效,因为输出矩阵的大小为 59578 行 × 21521 列,我想尽可能地避免计算成本。

有什么解决方案吗?到目前为止,我还没有在网上找到一个好的解决方案。

感谢您的帮助。

1个回答

在 PySpark 中执行此操作的方法是使用groupBypivot. 因为你不想做任何实际的聚合,只是枢轴,你可以first在这里使用。

from pyspark.sql.functions import first

(df.groupBy("Customer")
  .pivot("product")
  .agg(first("rating"))
  .fillna(0))

pivot当没有值时会给出空值,因此也fillna需要使用它来给出想要的结果。