【问题标题】:Remove special characters and substrings from strings in column从列中的字符串中删除特殊字符和子字符串
【发布时间】:2020-09-16 11:30:09
【问题描述】:

我对这一切都很陌生。我正在使用 Python 和 pandas 库来处理一个看起来像这样的大型数据集,例如:

           date                              text
0   Jul 31 2020       "test sentence numerouno"
1   Jul 31 2020       (second sentence) unonumero
2   Jul 31 2020       testuno sentence!!!

现在我正在寻找一个函数/循环,它可以删除一组已定义的子字符串以及特殊字符。

所以具体来说并坚持这个例子,我想从“文本”列中删除所有特殊字符 " ( ) ! 以及子字符串 uno

所以输出应该是这样的:

           date                         text
0   Jul 31 2020       test sentence numero
1   Jul 31 2020       second sentence numero
2   Jul 31 2020       test sentence

感谢您的帮助!

【问题讨论】:

    标签: python pandas substring


    【解决方案1】:

    您可以将str.replace 与以下模式一起使用:

    df['text'] = df['text'].str.replace(r'[^ A-Za-z]+|uno','')
    
    print(df.text)
    0      test sentence numero
    1    second sentence numero
    2             test sentence
    Name: text, dtype: object
    

    demo

    【讨论】:

    • 啊,好吧。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2011-04-11
    • 2016-01-23
    • 1970-01-01
    • 2019-11-09
    相关资源
    最近更新 更多