pyspark - 使用pyspark从1开始按递增顺序添加行号列
问题描述
我想创建一个名为“id”的列,其中包含要在最终生成对中使用的行号。“身份证”栏
我使用以下方式在python中完成了它。任何人都可以建议如何在pyspark中做到这一点。
con_2['id'] = range(1, 1+len(con_2))
len(con_2.customer_play_id.unique())
我的 Pyspark 代码如下,但它不起作用
from pyspark.sql.types import IntegerType
slen = udf(lambda s: len(s), IntegerType())
con_2 = con_2.withColumn('id', F.length(con_2.customer_play_id))
预期输出应该是(Id 是我要添加的列)df
id col1 col2
1 X Y
2 y1 y4
3 y2 y7
4 y3 y8
解决方案
from pyspark.sql.window import Window as W
from pyspark.sql import functions as
con_2 =con_2.withColumn("id",row_number().over(Window.orderBy("customer_play_id")))
con_2.show()
推荐阅读
- excel - Object Required Error On For Each Loop Only After Importing New Data
- c - 如何使用指向结构的指针进行迭代?
- javascript - atan() 返回楠
- html - 角度字段自动完成 HTML5 属性不起作用
- python - 我得到一个关键错误如何摆脱这个
- python - 我的代码在 PyCharm 中通过 main.py 运行,但不是从控制台运行。为什么会这样?
- c# - 我有一个供应商试图与我的 WCF api 通信,他们收到 http:500 错误,但 IIS 日志记录未显示任何详细信息
- spring-boot - 如何在 Spring Boot 中从应用程序属性文件中读取 jpa 命名查询?
- python - 尝试将熊猫系列数据传递给 date_range 函数时,无法将字符串转换为时间戳错误
- ios - Flutter iOS build Failed with Multiple commands 产生错误