Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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]’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading