为什么使用Spark Regexp_replace()时每个DF值都会改变?
我想在pyspark中使用regexp_replace()
将数据框架中的所有问号和后斜线转换为null值。这是我使用的代码:
question = "?"
empty_str = "\\\"\\\""
for column in df.columns:
df = df.withColumn(column, regexp_replace(column, question, None)
df = df.withColumn(column, regexp_replace(column, empty_str, None)
但是,当我使用此代码 all 时,我的数据框中的值会变成零值 - 不仅是问号和后斜线。有没有办法更改我的代码来解决此问题?
I want to use regexp_replace()
in PySpark to convert all question marks and back slashes in my data frame to null values. This is the code I used:
question = "?"
empty_str = "\\\"\\\""
for column in df.columns:
df = df.withColumn(column, regexp_replace(column, question, None)
df = df.withColumn(column, regexp_replace(column, empty_str, None)
However, when I use this code all the values in my dataframe turn into null values - not just the question marks and back slashes. Is there a way I can change my code to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
Regexp_replace
您不能将值替换为null,您将需要另一种方法,例如替换
in您的尝试,每个值都更改为null,因为您错误地向
替换
参数提供了错误,而不是str
。根据文档。With
regexp_replace
you cannot replace values to null, you will need another method, e.g.replace
In your attempt, every value changed to null, because you incorrectly provided None to the
replacement
argument, instead ofstr
. Only str is accepted, according to the documentation.它是这样工作的,ou必须使用\\用null替换backsslash和?用null替换问号
it is working like this, ou have to use \\ to replace backslash with null and ? to replace Question mark with null