将row_number添加到数据框pyspark中的连接列
Posted
技术标签:
【中文标题】将row_number添加到数据框pyspark中的连接列【英文标题】:Adding row_number to concatenated column in data frame pyspark 【发布时间】:2021-03-01 20:21:38 【问题描述】:我在pyspark
中有一个如下所示的数据框
df = sqlContext.createDataFrame(
[(1,'Y','Y',0,0,0,2,'Y','N','Y','Y'),
(2,'N','Y',2,1,2,3,'N','Y','Y','N'),
(3,'Y','N',3,1,0,0,'N','N','N','N'),
(4,'N','Y',5,0,1,0,'N','N','N','Y'),
(5,'Y','N',2,2,0,1,'Y','N','N','Y'),
(6,'Y','Y',0,0,3,6,'Y','N','Y','N'),
(7,'N','N',1,1,3,4,'N','Y','N','Y'),
(8,'Y','Y',1,1,2,0,'Y','Y','N','N')
],
('id', 'compatible', 'product', 'ios', 'pc', 'other', 'devices', 'customer', 'subscriber', 'circle', 'smb')
)
现在我想通过连接一些字符串在数据框中创建一个新列bt_string
。我已经完成了如下操作
import pyspark.sql.functions as f
from datetime import datetime
from time import strftime
from pyspark.sql import Window
# the below values will change as per requirement
job_id = '123'
sess_id = '99'
batch_id = '1'
time_now = datetime.now().strftime('%Y%m%d%H%M%S')
con_string = job_id + sess_id + batch_id + time_now + '000000000000000'
df1 = df.withColumn('bt_string', f.lit(con_string))
现在对于数据框,我想为每一行分配一个唯一编号。我应用了row_number
函数,如下所示
df2 = df1.withColumn("row_id",f.row_number().over(Window.partitionBy()))
输出如下
df2.show()
+---+----------+-------+---+---+-----+-------+--------+----------+------+---+--------------------+------+
| id|compatible|product|ios| pc|other|devices|customer|subscriber|circle|smb| bt_string|row_id|
+---+----------+-------+---+---+-----+-------+--------+----------+------+---+--------------------+------+
| 1| Y| Y| 0| 0| 0| 2| Y| N| Y| Y|12399120210301120...| 1|
| 2| N| Y| 2| 1| 2| 3| N| Y| Y| N|12399120210301120...| 2|
| 3| Y| N| 3| 1| 0| 0| N| N| N| N|12399120210301120...| 3|
| 4| N| Y| 5| 0| 1| 0| N| N| N| Y|12399120210301120...| 4|
| 5| Y| N| 2| 2| 0| 1| Y| N| N| Y|12399120210301120...| 5|
| 6| Y| Y| 0| 0| 3| 6| Y| N| Y| N|12399120210301120...| 6|
| 7| N| N| 1| 1| 3| 4| N| Y| N| Y|12399120210301120...| 7|
| 8| Y| Y| 1| 1| 2| 0| Y| Y| N| N|12399120210301120...| 8|
+---+----------+-------+---+---+-----+-------+--------+----------+------+---+--------------------+------+
现在我想将row_id
列添加到bt_string
列。我的意思是像下面这样
如果bt_string
的1st
行是
1239912021030112091500000000000000 then add the corresponding row_id value.
In the case of first row the value will be 1239912021030112091500000000000001
创建的新列应具有如下值
1239912021030112091500000000000001
1239912021030112091500000000000002
1239912021030112091500000000000003
1239912021030112091500000000000004
1239912021030112091500000000000005
1239912021030112091500000000000006
1239912021030112091500000000000007
1239912021030112091500000000000008
还需要确保列的长度应始终为35
个字符。
下面的字符串不应该超过35
个字符的长度
con_string = job_id + sess_id + batch_id + time_now + '000000000000000'
如果超过35
长度字符那么我们需要在上面的语句中添加trim
zeros
的个数。
我怎样才能达到我想要的效果
【问题讨论】:
可能类似于:`df2['new_column'] = df.apply(lambda row: str(int(row[bt_string]) + row['row_id'])? 即转换为一个整数,将它们相加,然后转换回字符串? 【参考方案1】:按照以下步骤来实现您的结果
# import necessary functions
import pyspark.sql.functions as f
from datetime import datetime
from time import strftime
from pyspark.sql import Window
# assign variables as per requirement
job_id = '123'
sess_id = '99'
batch_id = '1'
time_now = datetime.now().strftime('%Y%m%d%H%M%S')
# Join variables to get desired format of base string
con_string = job_id + sess_id + batch_id + time_now
# check length of base string and subtract from max length for that column 35
zero_to_add = 35 - len(con_string)
# Add the numbers of zeros based on the value received above
new_bt_string = con_string + zero_to_add * '0'
# add new column and convert column to decimal and then apply row_number
df1 = df.withColumn('bt_string', f.lit(new_bt_string).cast('decimal(35,0)'))\
.withColumn("row_id",f.row_number().over(Window.partitionBy()))
# add new column by sum of values from above added columns
df2 = df1.withColumn('bt_id', f.expr('bt_string + row_id'))
【讨论】:
以上是关于将row_number添加到数据框pyspark中的连接列的主要内容,如果未能解决你的问题,请参考以下文章
使用pyspark,spark + databricks时如何将完全不相关的列添加到数据框中
如何将 python 列表添加到 pyspark 列? [复制]