Databricks Scala从数据帧字段中删除间歇性\ n

时间:2019-11-16 19:28:06

标签: scala databricks

在scala中,我有一个具有字符串字段的数据框,在此字段中某些行的值具有断行换行(\ n)字符。 我想要做的就是最终得到一个数据框版本,只需删除“ \ n”即可。

command-358622955667885:15: error: value replace is not a member of org.apache.spark.sql.Column
    var t2df = tdf.withColumn("qty", tdf("Quantity").replace("\n","")).drop(m)
                                        ^

command-358622955667885:16: error: not found: value replace
    var t2df = tdf.withColumn("qty", replace(tdf("Quantity"), "\n", "")).drop(m)
                                     ^

1 个答案:

答案 0 :(得分:0)

import org.apache.spark.sql.functions._
t2df = t2df.withColumn("qty2", regexp_replace(col("qty"), "[\\r]", "")).drop("qty").withColumnRenamed("qty2","qty")

但是为什么必须使用正则表达式?

为什么我可以使用简单的替换?