Home > Enterprise >  remove text in a dataframe
remove text in a dataframe

Time:10-12

I have the dataframe df below: df:

Description      
sociis natoque (penatibus/magnis)
nec dui nunc mattis enim (ut/tellus/elementum)

I want to remove (penatibus/magnis) and (ut/tellus/elementum) from the description column

so i used

df["Description"] = df["Description"].str.replace("(penatibus/magnis)","")

I got this output:

Description      
sociis natoque ()
nec dui nunc mattis enim ()

What i want is this output:

Description      
sociis natoque
nec dui nunc mattis enim

CodePudding user response:

Feasible solution using regex:

df['Description'] =  [re.sub("[\(\[].*?[\)\]]", "", str(x)) for x in df['Description']]

This will remove any contents of (...) or [...], parenthesis included

CodePudding user response:

You can escpae values by /:

df["Description"] = df["Description"].str.replace(r'\(penatibus/magnis\)', '')

Or if need remove parantheses use:

df["Description"] = df["Description"].str.replace(r'\(.*\)', '')
  • Related