Remove texts that are enclosed in square brackets in Pandas

I would like to know how I can go through a column and remove the strings that are between square brackets.


‘String [more string]’



Can anyone help me?

I thought about using regular expressions. However, I don’t know how to zoom in. I think it can be done by writing a lambda function.

>Solution :

You might do it following way

import pandas as pd
df = pd.DataFrame({'col1':['String [more string]']})
df['col2'] = df['col1'].str.replace(r'\[.*\]', '', regex=True)

gives output

                   col1     col2
0  String [more string]  String 

Observe we need to escape [ to get literal [ as [ has special meaning otherwise.

Leave a Reply