apache-spark - 如何在 PySpark 中对 groupby 数据框应用条件
问题描述
我有一个这样的数据框:
ID Transaction_time Status final_time
1 1981-01-12 hit
1 1981-01-13 hit
1 1981-01-14 good 1981-01-15
1 1981-01-15 OK 1981-01-16
2 1981-01-06 good 1981-01-17
3 1981-01-07 hit 1981-01-16
4 1981-01-06 hit
4 1981-01-07 good
4 1981-01-08 good 1981-01-10
我想保留ID
如果:
Status
有“命中”和“好”/“好”- 最后
final_time
一个不为空Transaction_time
然后,我想提取:
id
- 这ID
status
- 最后Transaction_time
start_time
-Transaction_time
何时Status
从“命中”变为“好”finish_time
-final_time
最后Transaction_time
对于上面的示例,它将是:
id status start_time finish_time
1 OK 1981-01-14 1981-01-16
4 good 1981-01-07 1981-01-10
如何在 PySpark 中做到这一点?
解决方案
我主要使用窗口函数而不是 groupby:
w1 = Window.partitionBy('ID').orderBy(F.col('Transaction_time').desc())
w2 = Window.partitionBy('ID').orderBy(F.col('final_time').desc())
df2 = df1.withColumn('next_st', F.lag('Status', 1).over(w1)) \
.withColumn('next_tt', F.lag('Transaction_time', 1).over(w1)) \
.withColumn('max_tt', F.max('Transaction_time').over(w1)) \
.withColumn('max_ft', F.max('final_time').over(w2))
df3 = df2.join(df2.filter((F.col('Transaction_time') == F.col('max_tt')) & F.col('final_time').isNotNull()), 'ID', 'leftsemi')
df4 = df3.filter((F.col('Status') == 'hit') & F.col('next_st').isin(['good', 'OK']))
df5 = (
df4.alias('df4')
.join(df1.alias('df1'), (df1.ID == df4.ID) & (F.col('df1.final_time') == F.col('df4.max_ft')))
.select(
F.col('df4.ID').alias('id'),
F.col('df1.Status').alias('status'),
F.col('df4.next_tt').alias('start_time'),
F.col('df4.max_ft').alias('finish_time')
)
)
df5.show()
# +---+------+----------+-----------+
# | id|status|start_time|finish_time|
# +---+------+----------+-----------+
# | 4| good|1981-01-07| 1981-01-10|
# | 1| OK|1981-01-14| 1981-01-16|
# +---+------+----------+-----------+
进口:
from pyspark.sql import functions as F, Window
原始数据集:
data = [
(1, '1981-01-12', 'hit', None),
(1, '1981-01-13', 'hit', None),
(1, '1981-01-14', 'good', '1981-01-15'),
(1, '1981-01-15', 'OK', '1981-01-16'),
(2, '1981-01-06', 'good', '1981-01-17'),
(3, '1981-01-07', 'hit', '1981-01-16'),
(4, '1981-01-06', 'hit', None),
(4, '1981-01-07', 'good', None),
(4, '1981-01-08', 'good', '1981-01-10')]
df1 = spark.createDataFrame(data, ['ID', 'Transaction_time', 'Status', 'final_time'])
df1 = df1.withColumn('Transaction_time', F.col('Transaction_time').cast('date')) \
.withColumn('final_time', F.col('final_time').cast('date'))
df1.show()
# +---+----------------+------+----------+
# | ID|Transaction_time|Status|final_time|
# +---+----------------+------+----------+
# | 1| 1981-01-12| hit| null|
# | 1| 1981-01-13| hit| null|
# | 1| 1981-01-14| good|1981-01-15|
# | 1| 1981-01-15| OK|1981-01-16|
# | 2| 1981-01-06| good|1981-01-17|
# | 3| 1981-01-07| hit|1981-01-16|
# | 4| 1981-01-06| hit| null|
# | 4| 1981-01-07| good| null|
# | 4| 1981-01-08| good|1981-01-10|
# +---+----------------+------+----------+
中级dfs:
df1
+---+----------------+------+----------+
| ID|Transaction_time|Status|final_time|
+---+----------------+------+----------+
| 1| 1981-01-12| hit| null|
| 1| 1981-01-13| hit| null|
| 1| 1981-01-14| good|1981-01-15|
| 1| 1981-01-15| OK|1981-01-16|
| 2| 1981-01-06| good|1981-01-17|
| 3| 1981-01-07| hit|1981-01-16|
| 4| 1981-01-06| hit| null|
| 4| 1981-01-07| good| null|
| 4| 1981-01-08| good|1981-01-10|
+---+----------------+------+----------+
df2
+---+----------------+------+----------+-------+----------+----------+----------+
| ID|Transaction_time|Status|final_time|next_st| next_tt| max_tt| max_ft|
+---+----------------+------+----------+-------+----------+----------+----------+
| 1| 1981-01-15| OK|1981-01-16| null| null|1981-01-15|1981-01-16|
| 1| 1981-01-14| good|1981-01-15| OK|1981-01-15|1981-01-15|1981-01-16|
| 1| 1981-01-13| hit| null| good|1981-01-14|1981-01-15|1981-01-16|
| 1| 1981-01-12| hit| null| hit|1981-01-13|1981-01-15|1981-01-16|
| 3| 1981-01-07| hit|1981-01-16| null| null|1981-01-07|1981-01-16|
| 2| 1981-01-06| good|1981-01-17| null| null|1981-01-06|1981-01-17|
| 4| 1981-01-08| good|1981-01-10| null| null|1981-01-08|1981-01-10|
| 4| 1981-01-07| good| null| good|1981-01-08|1981-01-08|1981-01-10|
| 4| 1981-01-06| hit| null| good|1981-01-07|1981-01-08|1981-01-10|
+---+----------------+------+----------+-------+----------+----------+----------+
df3
+---+----------------+------+----------+-------+----------+----------+----------+
| ID|Transaction_time|Status|final_time|next_st| next_tt| max_tt| max_ft|
+---+----------------+------+----------+-------+----------+----------+----------+
| 1| 1981-01-15| OK|1981-01-16| null| null|1981-01-15|1981-01-16|
| 1| 1981-01-14| good|1981-01-15| OK|1981-01-15|1981-01-15|1981-01-16|
| 1| 1981-01-13| hit| null| good|1981-01-14|1981-01-15|1981-01-16|
| 1| 1981-01-12| hit| null| hit|1981-01-13|1981-01-15|1981-01-16|
| 3| 1981-01-07| hit|1981-01-16| null| null|1981-01-07|1981-01-16|
| 2| 1981-01-06| good|1981-01-17| null| null|1981-01-06|1981-01-17|
| 4| 1981-01-08| good|1981-01-10| null| null|1981-01-08|1981-01-10|
| 4| 1981-01-07| good| null| good|1981-01-08|1981-01-08|1981-01-10|
| 4| 1981-01-06| hit| null| good|1981-01-07|1981-01-08|1981-01-10|
+---+----------------+------+----------+-------+----------+----------+----------+
df4
+---+----------------+------+----------+-------+----------+----------+----------+
| ID|Transaction_time|Status|final_time|next_st| next_tt| max_tt| max_ft|
+---+----------------+------+----------+-------+----------+----------+----------+
| 1| 1981-01-13| hit| null| good|1981-01-14|1981-01-15|1981-01-16|
| 4| 1981-01-06| hit| null| good|1981-01-07|1981-01-08|1981-01-10|
+---+----------------+------+----------+-------+----------+----------+----------+
df5
+---+------+----------+-----------+
| id|status|start_time|finish_time|
+---+------+----------+-----------+
| 4| good|1981-01-07| 1981-01-10|
| 1| OK|1981-01-14| 1981-01-16|
+---+------+----------+-----------+
推荐阅读
- python - Pandas 到 csv 没有字符串引号和没有列表框
- powershell - 将带有 $ 的字符串传递给 Powershell 脚本参数
- jquery - 在页面加载时添加 preventCollision 和障碍选项
- testing - testcafe 中的 Html-testrail 报告不正确
- javascript - 如何从 fs.Dirent 获取类型?
- python - 我的 Python 不能使用 URL,没有人能弄清楚为什么?
- php - 查看最新版本的删除历史记录
- python - 关于微分方程的数学积分问题
- javascript - 如何在 mocha 测试的 for 循环中使用“它”
- sql-server - 用于查找在远程服务器上运行的服务的 Powershell 脚本