Pyspark 如何拆分linefeed上的DataFrame列值并使用最后2项(行)创建新列

Pyspark 如何拆分linefeed上的DataFrame列值并使用最后2项(行)创建新列,pyspark,apache-spark-sql,Pyspark,Apache Spark Sql,我想用换行符拆分一个列值,并用最后两项(行)创建一个新列 这不起作用(无值): 也许这是有帮助的- val sDF=Seq(““001\r\nLuc Krier\r\n2363南达科他州长湖瑞安路””, “002\r\nJeanny Thorn\r\n2263北卡罗来纳州罗利市巴顿巷”, “003\r\nDeddy E Beecher\r\n2839威斯康星州方德拉克哈特兰大道”, “004\r\nPhilippe Schauss\r\n1 Im Oberdorf Allemagne”, “00

我想用换行符拆分一个列值,并用最后两项(行)创建一个新列

这不起作用(无值):


也许这是有帮助的-

val sDF=Seq(““001\r\nLuc Krier\r\n2363南达科他州长湖瑞安路””,
“002\r\nJeanny Thorn\r\n2263北卡罗来纳州罗利市巴顿巷”,
“003\r\nDeddy E Beecher\r\n2839威斯康星州方德拉克哈特兰大道”,
“004\r\nPhilippe Schauss\r\n1 Im Oberdorf Allemagne”,
“005\r\nMinder I Tholen\r\nHagedoornweg 138 Amsterdam”“”。toDF(““s”“”)
val processedDF=sDF.withColumn(“col1”,slice(拆分(col(“s”),“\\r\\n”“”),-2,2))
processedDF.show(false)
processedDF.printSchema()
/**
* +--------------------------------------------------------------------+-------------------------------------------------------------+
*| s | col1|
* +--------------------------------------------------------------------+-------------------------------------------------------------+
*| 001\r\nLuc Krier\r\n2363南达科他州长湖瑞安路|[Luc Krier,南达科他州长湖瑞安路2363号]|
*| 002\r\nJeanny Thorn\r\n2263北卡罗莱纳州罗利巴顿巷|[Jeanny Thorn,北卡罗莱纳州罗利巴顿巷2263号]|
*| 003\r\nDeddy E Beecher\r\n2839威斯康星州方德拉克哈特兰大道|[特迪E Beecher,2839威斯康星州方德拉克哈特兰大道]|
*| 004\r\nPhilippe Schauss\r\n1我是Oberdorf Allemagne |[Philippe Schauss,1我是Oberdorf Allemagne]|
*| 005\r\n阿姆斯特丹托伦中心\r\nHagedoornweg 138 |[Meindert I Tholen,Hagedoornweg 138 Amsterdam]|
* +--------------------------------------------------------------------+-------------------------------------------------------------+
*
*根
*|--s:string(nullable=true)
*|--col1:array(nullable=true)
*| |--元素:字符串(containsnall=true)
*/

也许这是有帮助的-

val sDF=Seq(““001\r\nLuc Krier\r\n2363南达科他州长湖瑞安路””,
“002\r\nJeanny Thorn\r\n2263北卡罗来纳州罗利市巴顿巷”,
“003\r\nDeddy E Beecher\r\n2839威斯康星州方德拉克哈特兰大道”,
“004\r\nPhilippe Schauss\r\n1 Im Oberdorf Allemagne”,
“005\r\nMinder I Tholen\r\nHagedoornweg 138 Amsterdam”“”。toDF(““s”“”)
val processedDF=sDF.withColumn(“col1”,slice(拆分(col(“s”),“\\r\\n”“”),-2,2))
processedDF.show(false)
processedDF.printSchema()
/**
* +--------------------------------------------------------------------+-------------------------------------------------------------+
*| s | col1|
* +--------------------------------------------------------------------+-------------------------------------------------------------+
*| 001\r\nLuc Krier\r\n2363南达科他州长湖瑞安路|[Luc Krier,南达科他州长湖瑞安路2363号]|
*| 002\r\nJeanny Thorn\r\n2263北卡罗莱纳州罗利巴顿巷|[Jeanny Thorn,北卡罗莱纳州罗利巴顿巷2263号]|
*| 003\r\nDeddy E Beecher\r\n2839威斯康星州方德拉克哈特兰大道|[特迪E Beecher,2839威斯康星州方德拉克哈特兰大道]|
*| 004\r\nPhilippe Schauss\r\n1我是Oberdorf Allemagne |[Philippe Schauss,1我是Oberdorf Allemagne]|
*| 005\r\n阿姆斯特丹托伦中心\r\nHagedoornweg 138 |[Meindert I Tholen,Hagedoornweg 138 Amsterdam]|
* +--------------------------------------------------------------------+-------------------------------------------------------------+
*
*根
*|--s:string(nullable=true)
*|--col1:array(nullable=true)
*| |--元素:字符串(containsnall=true)
*/

只需将索引函数的子字符串作为

df1.withColumn('last2',f.substring_index('s','\r\n',-2)).drop('s').show(10,False)

+-----------------------------------------------------------+
|last2                                                      |
+-----------------------------------------------------------+
|Luc  Krier
2363  Ryan Road, Long Lake South Dakota        |
|Jeanny  Thorn
2263 Patton Lane Raleigh North Carolina     |
|Teddy E Beecher
2839 Hartland Avenue Fond Du Lac Wisconsin|
|Philippe  Schauss
1 Im Oberdorf Allemagne                 |
|Meindert I Tholen
Hagedoornweg 138 Amsterdam              |
+-----------------------------------------------------------+

希望它有帮助

您只需将索引函数的子字符串作为

df1.withColumn('last2',f.substring_index('s','\r\n',-2)).drop('s').show(10,False)

+-----------------------------------------------------------+
|last2                                                      |
+-----------------------------------------------------------+
|Luc  Krier
2363  Ryan Road, Long Lake South Dakota        |
|Jeanny  Thorn
2263 Patton Lane Raleigh North Carolina     |
|Teddy E Beecher
2839 Hartland Avenue Fond Du Lac Wisconsin|
|Philippe  Schauss
1 Im Oberdorf Allemagne                 |
|Meindert I Tholen
Hagedoornweg 138 Amsterdam              |
+-----------------------------------------------------------+

希望能有所帮助

是的,我也面临同样的负面索引问题,但正面索引是有效的。 我试过使用slice函数,效果很好。你能试试这个吗

import pyspark.sql.functions as F
df1 = sqlContext.createDataFrame([ ["001\r\nLuc Krier\r\n2363 Ryan Road, Long Lake South Dakota"], ["002\r\n\Jeanny Thorn\rn2263 Patton Lane Raleigh North Carolina"], ["003\r\nTeddy E Beecher\r\n2839 Hartland Avenue Fond Du Lac Wisconsin"], ["004\r\n\Philippe Schauss\r\n1 Im Oberdorf Allemagne"], ["005\r\n\Meindert I Tholen\r\nHagedoornweg 138 Amsterdam"] ]).toDF("s")
df_r = df1.withColumn('spl',F.split(F.col('s'),'\r\n'))
df_res = df_r.withColumn("res",F.slice(F.col("spl"),-1,1))

是的,我也面临同样的问题,负面索引,但正面索引工作。 我试过使用slice函数,效果很好。你能试试这个吗

import pyspark.sql.functions as F
df1 = sqlContext.createDataFrame([ ["001\r\nLuc Krier\r\n2363 Ryan Road, Long Lake South Dakota"], ["002\r\n\Jeanny Thorn\rn2263 Patton Lane Raleigh North Carolina"], ["003\r\nTeddy E Beecher\r\n2839 Hartland Avenue Fond Du Lac Wisconsin"], ["004\r\n\Philippe Schauss\r\n1 Im Oberdorf Allemagne"], ["005\r\n\Meindert I Tholen\r\nHagedoornweg 138 Amsterdam"] ]).toDF("s")
df_r = df1.withColumn('spl',F.split(F.col('s'),'\r\n'))
df_res = df_r.withColumn("res",F.slice(F.col("spl"),-1,1))

有样本数据吗?添加了简单样本。原始数据包含更多行。有样本数据吗?添加了简单样本。原始数据包含更多行。向下投票人…请在向下投票回答之前指定原因向下投票人…请在向下投票回答之前指定原因