【问题标题】:How can i strip the spaces with proper format?如何以正确的格式去除空格?
【发布时间】:2022-01-24 08:21:49
【问题描述】:

我有以下字符串-

  1.                                                              Meli Rif (                         kin.meli@gc.com                         ), Attorney, Regulatory Affairs Division, General Counsel, Pension Benefit Guaranty Corporation.
    
  2.                                                            Carol, Program Analyst, by telephone at; or by email at                          Carlomn@a.com.                                      
    

我使用下面的代码作为 -

df['strings'].strip()

但它返回的输出为 - 1.Meli Rif (kin.meli@gc.com),律师,监管事务部,总法律顾问,养老金福利担保公司

2.Carol,程序分析师,电话:或发送电子邮件至Carolmn@a.com。

在第二个字符串中,它删除所有空格 => 发送电子邮件至Carolmn@a.com

我怎样才能在条带化之前获得空间 ---- 希望这是 ---- 发送电子邮件至 Carolmn@a.com

【问题讨论】:

    标签: python pandas string dataframe strip


    【解决方案1】:

    使用str.replace:

    df['string'] = df['string'].str.replace(r'\s\s+', ' ', regex=True)
    

    【讨论】:

    • stackoverflow.com/users/15239951/corralien , TypeError: replace() 不接受关键字参数。得到错误?我把这个字符串作为一个字符串,就像我使用 XML 的 itertext 并放入一个变量一样。
    • 使用时立即工作 - ' '.join(string.split())。
    • 你如何在你的专栏中应用这个?
    • stackoverflow.com/users/15239951/corralien - 它是一个字符串而不是数据框列,所以我将其用作,结果 = ' '.join(text.split()) .
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    相关资源
    最近更新 更多