【问题标题】:Remove whitespaces in between strings in a column删除列中字符串之间的空格
【发布时间】:2023-01-31 01:19:07
【问题描述】:

我想从这样的单词中删除空格:

str1 = "101 - purple cloths"

str1sp = removespace(srt1)

print(str1sp) 

>>>>> 101-purple cloths

结果应该只去掉连字符 (-) 周围的空格。这将对整个列完成。

【问题讨论】:

  • df['column'] = df['column'].str.replace('\s-\s', '-')
  • 谢谢,成功了! @广黄

标签: python-3.x pandas removing-whitespace


【解决方案1】:

使用 str.replace 和 s*-s* 作为模式:

df['new'] = df['col'].str.replace(r's*-s*', '-', regex=True)

输出:

                   col                new
0  101 - purple cloths  101-purple cloths

regex demo

【讨论】:

    猜你喜欢
    • 2013-09-23
    • 1970-01-01
    • 2015-04-27
    • 2017-08-24
    • 2018-05-15
    • 1970-01-01
    • 2015-02-08
    • 2015-08-04
    • 2013-02-01
    相关资源
    最近更新 更多