我有一个数据框,其中某些行带有不必要的标点符号,例如?? 。等在字符串类型列的开头和结尾,如何使用Pandas删除那些标点符号?谢谢。
id price fruits
01 1 .apple
02 2 ,apple, banana?
03 3 ?orange?
应该是这样
id price fruits
01 1 apple
02 2 apple, banana
03 3 orange
答案 0 :(得分:3)
将str.strip
与 1.XXXX
2.XXXX
3.XXXX
一起使用:
punctuation
编辑:
对于自定义import string
df['fruits'] = df['fruits'].str.strip(string.punctuation)
print (df)
id price fruits
0 1 1 apple
1 2 2 apple, banana
2 3 3 orange
print (string.punctuation)
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
,可以使用字符串,如果要检查的值,则仅需要转义punctuation
:
"