How to remove special characters from a data column using the re module?

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 Band C. I used .transform(), but I want to do it using reif 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 rewhich would be the best way.

import re
#re.sub(r'\W+', '', your_string)
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.

+4
2

, map() lambda, , :

df['E'] = df['B'].map(lambda x: re.sub(r'\W+', '', x))

lambda . , . my_function = lambda x: x.my_method(3) def my_function(x): return x.my_method(3).

+5

lamda:

df['E'] = df['B'].str.replace('\W', '')
+2

Source: https://habr.com/ru/post/1612573/


All Articles