Search code examples
pythonpandaspython-re

How to remove special character for a single string with commas in it?


I need to remove a special character set (eg (x)) for a single string with commas in it.

Here is an example of the objective:

col1 col2
Brash (7), Confident (7), Street-Smart (6), Calm/Peaceful(5) Brash, Confident, Street-Smart, Calm/Peaceful

I've tried using the following codes:

df['col'] = df['col'].fillna('').astype(str).str.replace(r'[^A-Za-z ]', '', regex=True)
df['col'] = df['col'].str.replace(r" \(.*\)","")

But I'm only able to keep the first element or remove all the special characters, and I just need to remove the pattern (x)


Solution

  • df['col'].str.replace(r'\((\d+).*?\)', '', regex=True)