Pyspark 替换 Spark 数据框列中的字符串 [英] Pyspark replace strings in Spark dataframe column

查看:46
本文介绍了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

推荐答案

对于 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.

这篇关于Pyspark 替换 Spark 数据框列中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆