pyspark - Pyspark:用 groupby 替换逐行循环?
问题描述
我想通过基于数据框的一组列执行一组操作来生成特征。我的数据框看起来像:
root
|-- CreatedOn: string (nullable = true)
|-- ID: string (nullable = true)
|-- Industry: string (nullable = true)
|-- region: string (nullable = true)
|-- Customer: string (nullable = true)
例如。在过去 3/2/1 个月内使用 ID 和区域的次数。为此,我必须将整个数据帧扫描到当前记录。当前逻辑:
1. for i in df.collect() - Row-wise collect.
2. Filter the data 3 months before this row.
3. Generate features.
代码运行良好,但由于它是按行循环,因此运行时间超过 10 小时。有什么方法可以替换 Pyspark 中的逐行操作,因为它没有利用 pyspark 提供的并行性。像groupby这样的东西??
样本数据:
S.No ID CreatedOn Industry Region
1 ERP 05thMa2020 Communications USA
2 ERP 28thSept2020 Communications USA
3 ERP 15thOct2020 Communications Europe
4 ERP 15thNov2020 Communications Europe
5 Cloud 1stDec2020 Insurance Europe
考虑记录#4.. 特征 1 (Count_3monthsRegion):我想看看过去 3 个月内 ERP 在欧洲使用了多少次 (wrt CreatedOn)。答案是 1。(虽然记录#2 是 ERP 但在同一地区)
功能 2(Count_3monthsIndustry):我想看看过去 3 个月内 ERP 在通信中使用了多少次(wrt CreatedOn)。答案将是 2。
预期输出:
S.No ID CreatedOn Industry Region Count_3monthsRegion Count_3monthsIndustry
1 ERP 05thMay2020 Communications USA 0 0
2 ERP 28thSept2020 Communications USA 0 0
3 ERP 15thOct2020 Communications Europe 0 1
4 ERP 15thNov2020 Communications Europe 1 2
5 Cloud 1stDec2020 Insurance Europe 0 0
解决方案
推荐阅读
- java - 如何消除在 Windows 上运行的 Java Swing 中的黑色滞后区
- bash - ubuntu 18.04.2上的数字与字母数字排序
- c++ - 可能的 MSVC 编译器错误
- ios - 在 React Native 应用程序中使用 Firebase 身份验证时,auth.default.signInWithEmailAndPassword 不是一个函数
- java - request.getRemoteUser() 中的字符编码
- c# - 创建字典字典
存储在 Azure 表存储中 - c - 包含具有两个指针的节点结构的图
- visual-studio-code - VSCode Intellisense - 如何防止箭头键在建议中循环,直到按下 Tab
- sql-server - 如果一个日期为 NULL,则比较日期
- python - 查找 pandas 数据框中所有行对,时间在 60 秒内