如何从Spark数据框中的所有列中删除反斜杠?

编程入门 行业动态 更新时间:2024-10-13 18:22:34
本文介绍了如何从Spark数据框中的所有列中删除反斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何从Spark DF的多列中删除所有属于字符串的\字符?

How can I remove all \ characters that are a part of strings from multiple columns in a spark DF?

示例行:

11~ADX\|0.00\|ZZ\|BP\|WHT~SE\|41\|0064~GE\|0001\

预期输出:

11~ADX|0.00|ZZ|BP|WHT~SE|41|0064~GE|0001

推荐答案

在数据框的所有列上使用foldLeft,这样您就可以在每个单独的列上使用regexp_replace并返回最终的数据框.使用问题中的示例数据框(以下称为df),删除所有反斜杠:

Use foldLeft on all columns in the dataframe, in this way you can use regexp_replace on each separate column and return the final dataframe. Using the example dataframe in the question (called df below), to remove all backslashes:

val df2 = df.columns.foldLeft(df)((df, c) => df.withColumn(c, regexp_replace(col(c), "\\\\", "")))

您还可以通过以下操作转义所有反斜杠:

You could also escape all backslashes with the following:

val df2 = df.columns.foldLeft(df)((df, c) => df.withColumn(c, regexp_replace(col(c), "\\\\", "\\\\\\\\")))

如果不应该使用所有列,请创建一个单独的变量,其中包含要使用的列.要使用除一列(下面的列col)之外的所有所有列,请使用:

If not all columns should be used, create a separate variable containing the columns to use. To use all all columns except one (column col below) use:

val cols = df.columns diff List("col") cols.foldLeft ...

更多推荐

如何从Spark数据框中的所有列中删除反斜杠?

本文发布于:2023-11-06 08:17:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563171.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜杠   框中   数据   Spark

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!