Remove texts that are enclosed in square brackets in Pandas

Question:

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.

Answers:

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.

Answered By: Daweo

Right Bruno, you can use regular expressions and a lambda function to achieve this. Here’s an example code snippet in Python 3+

import re

# Define a regular expression to match square brackets and their contents
pattern = r'[.*?]'

# Define a lambda function to apply the regular expression and remove the matches
remove_square_brackets = lambda s: re.sub(pattern, '', s)

# Example list of strings
strings = ['String [more string]', 'Another [example] string', 'No square brackets here']

# Apply the lambda function to each string in the list using map()
result = list(map(remove_square_brackets, strings))

# Print the result
print(result)

#Output:
#['String ', 'Another  string', 'No square brackets here']

In this example, we define a regular expression pattern (r'[.?]’) that matches any string that starts with a "[" and ends with a "]" character, with any number of characters in between (using the ".?" pattern). We then define a lambda function that applies this regular expression to a given string and removes all matches. Finally, we use the map() function to apply this lambda function to each string in a list of example strings and store the result in a new list called the result.

Have Fun!

Answered By: Didierh
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.