Pyspark替换Spark数据框列中的字符串 [英] Pyspark replace strings in Spark dataframe column
本文介绍了Pyspark替换Spark数据框列中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过替换子字符串对Spark Dataframe列执行一些基本的处理.最快的方法是什么?
I'd like to perform some basic stemming on a Spark Dataframe column by replacing substrings. What's the quickest way to do this?
在我当前的用例中,我有一个要规范化的地址列表.例如以下数据框:
In my current use case, I have a list of addresses that I want to normalize. For example this dataframe:
id address
1 2 foo lane
2 10 bar lane
3 24 pants ln
将成为
id address
1 2 foo ln
2 10 bar ln
3 24 pants ln
推荐答案
For Spark 1.5 or later, you can use the functions package:
from pyspark.sql.functions import *
newDf = df.withColumn('address', regexp_replace('address', 'lane', 'ln'))
快速说明:
- 调用函数
withColumn
可以在数据框中添加(或替换,如果名称存在)列. - 函数
regexp_replace
将通过替换所有与模式匹配的子字符串来生成新列.
- The function
withColumn
is called to add (or replace, if the name exists) a column to the data frame. - The function
regexp_replace
will generate a new column by replacing all substrings that match the pattern.
这篇关于Pyspark替换Spark数据框列中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文