繁体   English   中英

在火花数据框中将字符串拆分为两列

[英]Split the string into two columns in spark dataframe

我有一个数据框,其行值是“我的名字是 Rahul”,我想将“我的名字是”拆分为一列,将“Rahul”拆分为另一列。 这里没有使用拆分功能的分隔符。 我怎样才能在火花中做到这一点?

在 Spark 中使用regexp_extract函数而不是Split函数。

Regex Explanation:

(.*)\\s(.*) //capture everything into 1 capture group until last space(\s) then capture everything after into 2 capture group.

Example:

val df= Seq(("My name is Rahul")).toDF("text") //sample string

df.withColumn("col1",regexp_extract($"text","(.*)\\s(.*)",1)).
withColumn("col2",regexp_extract($"text","(.*)\\s(.*)",2)).
show()

Result:

+----------------+----------+-----+
|            text|      col1| col2|
+----------------+----------+-----+
|My name is Rahul|My name is|Rahul|
+----------------+----------+-----+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM