如何删除空间和点并转换为小写

发布于 2025-02-06 08:08:15 字数 617 浏览 2 评论 0原文

我有一个pyspark数据框架,其中

N. Plainfield  
North Plainfield
West Home  Land 
NEWYORK
newyork
So. Plainfield
S.  Plaindield

一些名称包含姓名缩写之间的点和空格,而有些则没有。如何将它们转换为:(

 n Plainfield  
 north plainfield
 west homeland
 newyork 
 newyork
 so plainfield
 s plainfield

在缩写和名称之间没有点和1个空间之间没有点和空间)

我尝试使用以下内容,但它只能替换点,而不会在缩写之间删除空格:

names_modified = names.withColumn("name_clean", regexp_replace("name", r"\.",""))

删除了whitespaces和dots之后有任何方式获得不同的值。 像这样。

north plainfield
west homeland 
newyork
so plainfield
 

I have a pyspark dataframe with names like

N. Plainfield  
North Plainfield
West Home  Land 
NEWYORK
newyork
So. Plainfield
S.  Plaindield

Some of them contain dots and spaces between initials, and some do not. How can they be converted to:

 n Plainfield  
 north plainfield
 west homeland
 newyork 
 newyork
 so plainfield
 s plainfield

(with no dots and spaces between initials and 1 space between initials and name)

I tried using the following, but it only replaces dots and doesn't remove spaces between initials:

names_modified = names.withColumn("name_clean", regexp_replace("name", r"\.",""))

After removing the whitespaces and dots is there any way get the distinct values.
Like this.

north plainfield
west homeland 
newyork
so plainfield
 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九局 2025-02-13 08:08:17

我认为您应该分开步骤。

  1. 从大写到小写

  2. 使用Regex_replace函数替换点

     来自pyspark.sql.functions导入 *
    
     #从大写到小写
     names_modified = names_modified.withcolumn('name',lower('name'))
     #从点到眨眼
     names_modified = names_modified.withcolumn('name_clean',regex_replace('name',','。',''))
     

I think you should divide the step.

  1. from uppercase to lowercase

  2. replace dot using regex_replace function

     from pyspark.sql.functions import *
    
     # from uppercase to lowercase
     names_modified = names_modified.withColumn('name', lower('name'))
     # from dot to blink
     names_modified = names_modified.withColumn('name_clean', regex_replace('name', '.', ' '))
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文