SK *_*yad 5 scala startswith apache-spark apache-spark-sql
我有一个包含 500 万条记录的数据集,我需要使用提供多个或和条件来替换列中的所有值startsWith()。
此代码适用于单一条件:
df2.withColumn("Deposits", when(col("Deposits").startsWith("0"),"1.1").otherwise(col("Deposits"))).show()
Run Code Online (Sandbox Code Playgroud)
我需要这样做:
df2.withColumn("Deposits", when(col("Deposits").startsWith("0"||"1"),"1.1").otherwise(col("Deposits")))
Run Code Online (Sandbox Code Playgroud)
rlike与正则表达式一起使用^(1|0)(以 1 或 0 开头):
df2.withColumn(
"Deposits",
when(col("Deposits").rlike("^(1|0)"), "1.1").otherwise(col("Deposits"))
).show
Run Code Online (Sandbox Code Playgroud)
如果您想Deposits根据 startwith 条件更新列,您可以链接多个 when 表达式,如下所示:
val depositsCol = Seq(
("1", "1.1"),
("2", "1.2")
).foldLeft(col("Deposits")) { case (acc, (start, value)) =>
when(col("Deposits").startsWith(start), value).otherwise(acc)
}
df2.withColumn("Deposits", depositsCol).show
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2321 次 |
| 最近记录: |