1

I have a data, where there are words in some rows. For example:

Test String

(Test1) String

Test (String1)

I need to find a substring in brackets using pandas. So, output here will be ['Test1', 'String1']

I tried something like this, but I can't find a word exactly in brackets.

df['column'].str.extract('([A-Z]\w{0,})')
2
  • Please post sample data and what you're trying to extract with desired output Commented Jan 28, 2016 at 11:44
  • @EdChum I've added sample Commented Jan 28, 2016 at 11:47

1 Answer 1

1

You can use the following regex pattern:

In [180]:
df['text'].str.extract(r'\((\w+)\)')

Out[180]:
0        NaN
1      Test1
2    String1
Name: text, dtype: object

So this looks for any words that are present in brackets, here brackets need to be escaped \( for example, we also want to find all words so w+ is needed here.

If you want a list you can call dropna and then tolist:

In [185]:
df['text'].str.extract(r'\((\w+)\)').dropna().tolist()

Out[185]:
['Test1', 'String1']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.