2

My df has 2 columns:

Name    Attr
a(bc)
b(aca)
(cba)

I would like the column Attr to have the values within parentheses of column Name

Name    Attr
a(bc)   bc
b(aca)  aca
(cba)   cba

I tried:

df['Attr'] = re.findall('\(.*?\)',df['Name'].astype('str'))

TypeError: expected string or buffer

Really appreciate any help

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
Square9627
  • 869
  • 3
  • 10
  • 15

1 Answers1

9

Use str.extract:

df['Attr'] = df['Name'].str.extract(r"\(([A-Za-z]+)\)", expand=False)
print (df)
     Name Attr
0   a(bc)   bc
1  b(aca)  aca
2   (cba)  cba

Or add () to your regex:

df['Attr'] = df['Name'].str.extract(r"\((.*?)\)", expand=False)
print (df)
     Name Attr
0   a(bc)   bc
1  b(aca)  aca
2   (cba)  cba
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252