scala - 在火花数据框中将字符串拆分为两列
问题描述
我有一个数据框,其行值“我的名字是 Rahul”我想将“我的名字是”拆分为一列,将“Rahul”拆分为另一列。这里没有使用 split 函数的分隔符。我怎样才能在火花中做到这一点?
解决方案
在 Spark 中Split
使用函数而不是函数。regexp_extract
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|
+----------------+----------+-----+
推荐阅读
- django - 如何使用 django 模型 clean() 方法?
- java - Java 11 中的泛型有什么问题吗?
- python - 将自定义对数刻度位置添加到 matplotlib
- office365 - “获取当前状态 - Office 365 服务通信 API”输出的状态“VerifyingService”定义
- python - 如何使用python创建图像_FILES数组来上传图像
- php - 基于 Web 数据更改的 Android 应用程序即时通知
- docker - 如何将自定义 docker 映像与 Azure Databricks 一起使用
- javascript - 与其他 GLTF 查看器相比,Javascript Three.Js GLTF 加载非常慢
- postgresql - postgresql表中的累积和
- python - 从 HTTP/2 数据包中提取 Pyshark 标头“流 ID”信息