0

I have a Dataframe as follows:

df
   title     TestData  Value 
0      A  Test1(data)    1.0 
1      B     t(data2)    2.2 
2      C   Ts(data 3)    3.3 

I want to extract from the entire TestData column the data within parenthesis as follows:

   title  TestData  Value 
0      A      data    1.0 
1      B     data2    2.2 
2      C    data 3    3.3 

The index of the parenthesis keeps changing so does the length of the string within parenthesis. Any idea of how to do it?

  • 1
    Duplicate of https://stackoverflow.com/questions/16842001/copy-text-between-parentheses-in-pandas-dataframe-column-into-another-column – Zero Jan 19 '18 at 14:15

2 Answers2

4

I think you can use str.extract:

df['TestData'] = df['TestData'].str.extract(r"\((.*)\)", expand=False)
print (df)
  title TestData  Value
0     A     data    1.0
1     B    data2    2.2
2     C   data 3    3.3
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

Or you can using str.split

df.TestData=df.TestData.str.split(pat='[(|)]',expand=True)[1]
df
Out[129]: 
  TestData  Value title
0     data      1     A
1    data2      2     B
2   data 3      3     C
BENY
  • 317,841
  • 20
  • 164
  • 234