pyspark - 创建一个标记客户的新列
问题描述
我的目标是汇总 customerID(计数),创建一个新列并标记经常返回文章的客户。我怎样才能做到这一点?(使用 Databricks,pyspark)
train.select("itemID","customerID","returnShipment").show(10)
+------+----------+--------------+
|itemID|customerID|returnShipment|
+------+----------+--------------+
| 186| 794| 0|
| 71| 794| 1|
| 71| 794| 1|
| 32| 850| 1|
| 32| 850| 1|
| 57| 850| 1|
| 2| 850| 1|
| 259| 850| 1|
| 603| 850| 1|
| 259| 850| 1|
+------+----------+--------------+
解决方案
您可以定义一个阈值,然后将此阈值与returnShipments
每个的总和进行比较customerID
:
from pyspark.sql import functions as F
threshold=5
df.groupBy("customerID")\
.sum("returnShipment") \
.withColumn("mark", F.col("sum(returnShipment)") > threshold) \
.show()
推荐阅读
- algorithm - 如何计算二维矩阵中东方数字的总和?
- android - 如何解决 UnsatisfiedLinkError?
- unity3d - Unity - VR - 应用切换器?
- c++ - 如何将整数分配给 char 类型的变量
- java - 如何修改 swagger-maven-plugin 中使用的默认 Jackson 映射器
- reactjs - 在 react-google-maps 中按道具居中和缩放?
- intersystems-cache - 更新此列时更新其他列的值
- ruby-on-rails - 文件上传不是将参数设置为文件,而是设置为字符串
- javascript - 单选按钮不会更新 NavItem 中的 UI
- java - 为什么“res.add(new ArrayList<>(list));” 这里?