如果首先,如何将SAS代码逻辑转换为Pyspark?
我正在尝试将以下SAS逻辑转换为Pyspark,但我没有得到预期的输出。
if first.loan then seq_id = 0;
seq_id+1
The current dataset:
loan | module |
---|---|
743 | 455 |
4490 | 795 |
1101 | 235 |
1101 | 335 |
1101 | 435 |
3471 | 898 |
The expected dataset:
loan | module | seq_id |
---|---|---|
743 | 455 | 1 |
4490 | 795 | 1 |
1101 | 235 | 1 |
1101 | 335 | 2 |
1101 | 435 | 3 |
3471 | 898 | 1 |
I'm trying to convert the following SAS logic to PySpark, but I'm not getting the expected output.
if first.loan then seq_id = 0;
seq_id+1
The current dataset:
loan | module |
---|---|
743 | 455 |
4490 | 795 |
1101 | 235 |
1101 | 335 |
1101 | 435 |
3471 | 898 |
The expected dataset:
loan | module | seq_id |
---|---|---|
743 | 455 | 1 |
4490 | 795 | 1 |
1101 | 235 | 1 |
1101 | 335 | 2 |
1101 | 435 | 3 |
3471 | 898 | 1 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于组中的第一个值,您分配
seq_id = 0
,然后立即使用seq_id+1
更改它。一对一组中的后续值使用seq_id+1
更改,因此有效地在每个组中创建行号。在Spark中,可以使用
For the first value in the group, you assign
seq_id=0
and then you immediately change it usingseq_id+1
. Subsequent values in the group one-by-one get changed usingseq_id+1
, so effectively you create row numbers in every group.In Spark, this can be done using
row_number
window function.