如何解决使用pyspark中的条件通过聚合创建列表
嗨,我是 pyspark 的新手。
我的数据框如下所示:
+--------------------+----------+--------------+--------------------+----------------------+-------+
| cookieId|sessionSeq|sessionUserSeq| time | keyword | code |
+--------------------+----------+--------------+--------------------+----------------------+-------+
|03bdc154-3261-0a9...| 4| 3| 2020-12-12 04:51 | X-mas tree | null|
|03bdc154-3261-0a9...| 4| 4| 2020-12-12 04:52 | X-mas tree | null|
|03bdc154-3261-0a9...| 4| 4| 2020-12-12 04:53 | null | 5027|
|03bdc154-3261-0a9...| 4| 7| 2020-12-12 04:54 | x-mas tree | null|
|03bdc154-3261-0a9...| 4| 9| 2020-12-12 04:55 | bulb | null|
|017224a2-2d65-23e...| 8| 2| 2020-12-11 05:04 | X-mas tree | null|
|017224a2-2d65-23e...| 8| 3| 2020-12-11 05:05 | X-mas decoration | null|
|017224a2-2d65-23e...| 8| 3| 2020-12-11 05:06 | null | 5028|
|017224a2-2d65-23e...| 8| 8| 2020-12-11 05:07 | X-mas decoration | null|
+--------------------+----------+--------------+--------------------+----------------------+-------+
我想通过按“cookieId”和“代码”对数据框进行分组来制作关键字列表。这里重要的一点是,当'code'列中有一个值时,'keyword_list'只对小于当时时间的次数进行制作。
预期输出:
+------------+-------------------------+-----------------------------------+
| code | cookieId | keyword_list |
+--------------------------------------+-----------------------------------+
| 5027 | 03bdc154-3261-0a9... | [X-mas tree,X-mass tree] |
| 5028 | 017224a2-2d65-23e... | [X-mas tree,X-mas decoration] |
+------------+-------------------------------------------------------------+
我尝试了很多,但没有得到想要的结果。请帮帮我...!
解决方法
使用if A>X:
print('The no is smaller than you chose')
elif A<X: # Here!
print('The no is larger than you chose')
else:
break
获取代码,然后使用first
聚合。
collect_list
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。