【问题标题】:Removing only specified special characters from a column [duplicate]从列中仅删除指定的特殊字符[重复]
【发布时间】:2022-07-19 21:44:39
【问题描述】:

我的数据框中有 3 列,我想只从下面提到的列中删除那些特殊字符:

,.-=[]{}/?,.()&^%$#@!;~`*

我已经尝试了下面的代码,但它不能正常工作

regex = re.compile('[,.-=[]{}\/?,.<>()*&^%$#@!;~`]')
s=[]

for i in range(len(df1)):
    L = df1.loc[i,'Vendor Name']
    s.append(regex.sub('', L))
   

df1['Vendor Name']=s

此代码没有删除指定的特殊字符,我无法找出问题所在。

【问题讨论】:

  • 可能是df1['Vendor Name'].str.replace(r'[][,.={}/?,.&lt;&gt;()*&amp;^%$#@!;~`-]+', '', regex=True)?你还需要删除\ char 吗?

标签: python pandas regex special-characters


【解决方案1】:

试试这个:

df['Vendor Name'].apply(lambda x: x.replace('[,.-=[]{}\/?,.<>()*&^%$#@!;~`]+', '', regex=True) )

【讨论】:

    猜你喜欢
    • 2022-01-25
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 2014-05-20
    • 2020-11-19
    • 2012-12-16
    • 1970-01-01
    相关资源
    最近更新 更多