我有以下数据框:
timestamp sum
31/01/2017 09:00 0
31/01/2017 10:00 0
31/01/2017 11:00 0
31/01/2017 12:00 2
31/01/2017 13:00 2
31/01/2017 14:00 2
31/01/2017 15:00 11
并想添加一个新的Id列-只是一个像这样的运行编号:
+----------------+---+---------+
| timestamp|sum|running_id|
+----------------+---+---------+
|2017-01-31 09:00| 0| 0|
|2017-01-31 10:00| 0| 1|
|2017-01-31 11:00| 0| 2|
|2017-01-31 12:00| 2| 3|
|2017-01-31 13:00| 2| 4|
|2017-01-31 14:00| 2| 5|
|2017-01-31 15:00| 11| 6|
我这样做是这样的:
sub_data_spark = sub_data_spark.rdd.zipWithIndex().map(lambda x: (x[0][0],x[0][1],x[1])).toDF(sub_data_spark.columns+["running_id"])
有人可以建议一种“更清洁”的方式吗?
谢谢鲍里斯
尝试使用 select *, row_Number() over ( order by sum) from table
或基于逻辑的任何列。也可以使用PARTITION BY子句。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句