且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Pyspark 替换 Spark 数据框列中的字符串

更新时间:2023-11-13 23:28:40

对于 Spark 1.5 或更高版本,您可以使用 functions 包:

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.