Bitopan Gogoi
Bitopan Gogoi

Reputation: 125

Removing the characters '*' and '\' from dataframe elements

I am trying to remove the following characters from my dataframe- {space, ", ', \n, * and }. I am able to do it for the first four characters, but getting error for * and . Please help. The code I am using is-

df = df.replace(to_replace = ["'",'"'," ","\n","*","\"], value = ["","","","",""], regex=True)

also tried "\" inplace of "", still getting error.

Upvotes: 1

Views: 21

Answers (1)

jezrael
jezrael

Reputation: 863166

Try espape values by \ for special regex characters like *:

df = pd.DataFrame({'a':['ss""ss',"dd'e",'ww\n','rrr*\\']})
df = df.replace(["'",'"'," ",r"\n",r"\*",r"\\"], '', regex=True)
print (df)
      a
0  ssss
1   dde
2    ww
3   rrr

Upvotes: 3

Related Questions