How to remove special characers from a column of dataframe using module re?
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)
.
A one liner without map
is:
df['E'] = df['B'].str.replace('\W', '')