簡體   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