Hey I have seen that link but nowhere there they have used re
module that's why I have posted here. Hope you understand and remove the duplicate.
Here is the Link. I want to use re
module.
Table:
A B C D
1 Q! W@ 2
2 1$ E% 3
3 S2# D! 4
here I want to remove the special characters from column
B
and C
. I have used .transform()
but I want to do it using re
if possible but I am getting 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:
df['E'] = df['B'].str.translate(None, ",!.; -@!%^&*)(")
It's working only if I know what are the special characters.
But I want to use re
which would be the best way.
import re
#re.sub(r'\W+', '', your_string)
df['E'] = re.sub(r'\W+', '', df['B'].str)
Here I am getting error:
TypeError: expected string or buffer
So how should I pass the value to get the correct output.