我有一个3列的数据帧。我想操作的两个列是Dog_Summary
和{
我试过以下方法:
df[['Dog_Summary', 'Dog_Description']] = df[['Dog_Summary', 'Dog_Description']].apply(lambda x: x.str.translate(None, string.punctuation))
对于上述内容,我得到一个错误信息:
^{pr2}$我尝试的第二种方法是:
df[['Dog_Summary', 'Dog_Description']] = df[['Dog_Summary', 'Dog_Description']].apply(lambda x: x.replace(string.punctuation, ' '))
但是,它仍然不起作用!
有人能给我建议吗
谢谢!:)
您可以使用正则表达式和
string.punctuation
来执行此操作:
^{pr2}$.str.replace()
的第一个参数可以是正则表达式。在这种情况下,可以使用f字符串和character class来捕获任何标点字符:如果要将此应用于数据帧,只需按照您现在所做的操作:
或者,您可以使用
s.replace(rf'[{punctuation}]', '', regex=True)
(不使用.str
访问器)。在相关问题 更多 >
编程相关推荐