Search code examples
pythonpandaspython-re

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.

Example

'String [more string]'

after

'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)
    print(df)
    

    gives output

                       col1     col2
    0  String [more string]  String 
    

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