Hey, I saw this link, but they didn't use the module anywhere re
, so I posted it here. I hope you understand and delete the duplicate.
Here's the link . I want to use the module re
.
Table:
A B C D
1 Q! W@ 2
2 1$ E% 3
3 S2# D! 4
here I want to remove special characters from column
B
and C
. I used .transform()
, but I want to do it using re
if possible, but I get errors.
Output:
A B C D E F
1 Q! W@ 2 Q W
2 1$ E% 3 1 E
3 S2# D! 4 S2 D
My code is:
df['E'] = df['B'].str.translate(None, ",!.; -@!%^&*)(")
Only works if I know what the special characters are.
But I want to use re
which would be the best way.
import re
df['E'] = re.sub(r'\W+', '', df['B'].str)
Here I get the error message:
TypeError: expected string or buffer
So, how do I pass the value to get the correct output.