我想通过替换子字符串对 Spark 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
对于 Spark 1.5 或更高版本,您可以使用 functions 包:
from pyspark.sql.functions import *
newDf = df.withColumn('address', regexp_replace('address', 'lane', 'ln'))
快速解释:
调用函数 withColumn 向数据框中添加(或替换,如果名称存在)一列。
函数 regexp_replace 将通过替换与模式匹配的所有子字符串来生成一个新列。
对于斯卡拉
import org.apache.spark.sql.functions.regexp_replace
import org.apache.spark.sql.functions.col
data.withColumn("addr_new", regexp_replace(col("addr_line"), "\\*", ""))