Extract all contents inside brackets (including the brackets) in a data frame column

Viewed 479

I have this column of data in a DataFrame (just shown here in a CSV file):

What I am trying to do is extract all the data within the brackets and also include the brackets so each row looks something like this

[GO:0005524],[GO:0000287],[GO:0004709],[GO:0004674],etc...

This is the code I have so far but I always end up with a blank column:

df['go_molecular_function'] = df['go_molecular_function'].str.extract(r"\((A-Za-z+)\)", expand=False)
1 Answers

I figured it out I used this code

df['go_molecular_function'] = df['go_molecular_function'].str.findall(r"(?<=\[)([^]]+)(?=\])")

and ended up with this which I am happy with

Related