Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove special characers from a column of dataframe using module re? [duplicate]

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.

like image 354
Rahul Shrivastava Avatar asked Oct 21 '15 10:10

Rahul Shrivastava


2 Answers

A one liner without map is:

df['E'] = df['B'].str.replace('\W', '')
like image 70
Amir Imani Avatar answered Nov 16 '22 06:11

Amir Imani


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).

like image 22
TigerhawkT3 Avatar answered Nov 16 '22 08:11

TigerhawkT3