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.
A one liner without map is:
df['E'] = df['B'].str.replace('\W', '')
As this answer shows, you can use map() with a lambda function that will assemble and return any expression you like:
df['E'] = df['B'].map(lambda x: re.sub(r'\W+', '', x))
lambda simply defines anonymous functions. You can leave them anonymous, or assign them to a reference like any other object. my_function = lambda x: x.my_method(3) is equivalent to def my_function(x): return x.my_method(3).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With