[英]Pyspark replace strings in Spark dataframe column by using values in another column
[英]Pyspark replace strings in Spark dataframe column
我想通過替換子字符串在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或更高版本,您可以使用函數包:
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"), "\\*", ""))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.