Copy text between parentheses in pandas DataFrame column into another column

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

Leave a Comment