7

I am trying to copy text that appears between parentheses in a pandas DataFrame column into another column. I have come across this solution to parse strings accordingly: Regular expression to return text between parenthesis

I would like to assign the result element-by-element to the same row in a new column. However, this doesn't carry over directly to pandas Series. I seems that map/apply/lambda seems the way to go. I've arrived at this piece of code, but getting an invalid syntax error.

dataSources.dataUnits = dataSources.dataDescription.map(str.find("(")+1:str.find(")"))

Obviously, I'm not yet fluent enough there - help much appreciated.

Community
  • 1
  • 1
Stefan
  • 41,759
  • 13
  • 76
  • 81

1 Answers1

22

You can just use an apply with the same method suggested there:

In [11]: s = pd.Series(['hi(pandas)there'])

In [12]: s
Out[12]:
0    hi(pandas)there
dtype: object

In [13]: s.apply(lambda st: st[st.find("(")+1:st.find(")")])
Out[13]:
0    pandas
dtype: object

Or perhaps you could use one of the Series string methods e.g. replace:

In [14]: s.str.replace(r'[^(]*\(|\)[^)]*', '')
Out[14]:
0    pandas
dtype: object

throw away all the stuff before the ( and all the stuff after ) inclusive.

From 0.13 you can use the extract method:

In [15]: s.str.extract('.*\((.*)\).*')
Out[15]: 
0    pandas
dtype: object
Community
  • 1
  • 1
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
  • Just one question: rows without parenthesized content return the entire field (fields are enclosed by ''). How to avoid this / skip rows where there is nothing to be found? – Stefan May 30 '13 at 20:10
  • 1
    Hmm not too sure, think you may be better off just with the apply, but something using match could be possible: `s.str.findall(r'(?<=\()[^(]*(?=\))')` – Andy Hayden May 30 '13 at 20:49
  • The issue came up with apply in fact. Findall works! Last thing - how do I get rid of the square brackets around the results other than stripping them after the fact? – Stefan May 30 '13 at 21:00
  • @AndyHayden what if we want to get only digits from bracket values i.e. "Titanic (1997) (Drama)" . It is giving me Drama instead of 1997. I want to extract only year. How can I do that?? – MegaBytes Jun 14 '16 at 07:30
  • 1
    @MegaBytes maybe use \d+ rather than [^(]* – Andy Hayden Jun 14 '16 at 16:00
  • 1
    Thanks @AndyHayden I used this one which works for me `data['Title'].str.extract('.*\((.*\d{4})\).*')` – MegaBytes Jun 15 '16 at 07:38