Python Forum

Full Version: Replacing with Regex
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys

I'm trying to figure out how to replace a string in dataframe using regex's match

In my case I want to get rid of parenthesis and what is inside it

Example: What I have

Name
1 John
2 Lucy Smart
3 Nick (Sweet)

What I expect to get

Name
1 John
2 Lucy Smart
3 Nick

I need something like:
users['Name'].replace(to_replace=r"(?P<f_name>\w+)(?P<l_name>\w+)(?P<par>\(\w+\))", value='<f_name> <l_name>', inplace=True, regex=True)

But I can't figure out how to connect between a value field and to_replace

Anyone can help?

Thanks
Naama
Do you just need a general regex to remove paren and its contents?
>>> re.sub(r'\([^)]*\)', '', '3 Nick (Sweet)')
'3 Nick '
>>> re.sub(r'\([^)]*\)', '', '3 Nick Sweet')
'3 Nick Sweet'
Thanks!
That's what I need!