【发布时间】:2018-06-06 07:15:34
【问题描述】:
我有一个类似的数据框:
df = spark.createDataFrame([(0, "departmentcode__50~#~p99189h8pk0__10483~#~prod_productcolor__Dustysalmon Pink","departmentcode__50~#~p99189h8pk0__10483~#~prod_productcolor__Dustysalmon Blue"), (1, "departmentcode__10~#~p99189h8pk0__10484~#~prod_productcolor__Dustysalmon Black","departmentcode__50~#~p99189h8pk0__10483~#~prod_productcolor__Dustysalmon Blue"), (2, "departmentcode__60~#~p99189h8pk0__10485~#~prod_productcolor__Dustysalmon White","departmentcode__50~#~p99189h8pk0__10483~#~prod_productcolor__Dustysalmon Blue")], ["id", "items_base", "item_target"])
我需要一个类似于以下的新数据框:
+---+-----------------+----------------+--------+------+
|id |dept0 |att0 |position|flag |
+---+-----------------+----------------+--------+------+
|0 |departmentcode |50 |1 |Base |
|0 |p99189h8pk0 |10483 |2 |Base |
|0 |prod_productcolor|Dustysalmon Pink|3 |Base |
|0 |departmentcode |50 |1 |Target|
|0 |p99189h8pk0 |10483 |2 |Target|
|0 |prod_productcolor|Dustysalmon Blue|3 |Target|
|1 |departmentcode |10 |1 |Base |
...
...
+---+-----------------+----------------+--------+------+
我用 ~#~ 和 __ 拆分 items_base 和 item_target 并创建新的 6 行。 items_base 3行,item_target 3行(其中position为dept0拆分后的位置,flag表示是items_base还是items_target)
【问题讨论】:
标签: python-3.x apache-spark pyspark apache-spark-sql