1

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.

1 Answer 1

3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.