使用pyspark在数据框中移动插槽

问题描述

我有一个数据框,如下所示,有四列,对于每个客户,我都有12行,其中包含以下详细信息,如示例所示

Cust_id|slot|trigger_id|coup_type
1|       1| 2101| null
1|       2| 2102| null
1|       3| 2103| null
1|       4| 2104| null
1|       5| 2105| product
1|       6| 2106| null
1|       7| 2107| null
1|       8| 2108| product
1|       9| 2109| null
1|       10| 21010| null
1|       11| 21011| product
1|       12| 21012| product

现在,我需要基于coup_type执行插槽移位,以便所有产品妙用都应出现在插槽7-10中,并且分配应始终从插槽7开始。移位后的插槽应重新排列,如下所示:-

Cust_id|slot|trigger_id|coup_type
1|       1| 2101| null
1|       2| 2102| null
1|       3| 2103| null
1|       4| 2104| null
1|       5| 2105| null
1|       6| 2106| null
1|       7| 2105| product
1|       8| 2108| product
1|       9| 21011| product
1|       10| 21012| product
1|       11| 21009| null
1|       12| 21010| null

我需要在pyspark中执行此操作。

如果问题不清楚,请让我知道。

谢谢。

解决方法

您可以通过对列`^(https|ftp|http|ftps):\/\/([a-z\d_]+\.)?(([a-zA-Z\d_]+)(\.[a-zA-Z]{2,6}))(\/[a-zA-Z\d_\%\-=\+]+)*(\?)?([a-zA-Z\d=_\+\%\-&\{\}\:]+)?` 进行排序来获得row_number,并使其行号作为与coup_type列的连接键。如果该列不是连续的,那么您可能需要另一列作为slot列的排序行号。

slot