我在下面有一个数据框 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
增加数量。
我怎样才能实现自己想要的
请按照以下步骤操作来达到目的
# 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'))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句