问题
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
回答1:
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
来源:https://stackoverflow.com/questions/37920443/pandas-get-values-within-parentheses-of-a-panda-dataframe-column