我试图简单地删除'('和')'从熊猫专栏系列的开头和结尾开始。这是我到目前为止最好的猜测,但它只返回空字符串,并且()完好无损。
postings['location'].replace('[^\(.*\)?]','', regex=True)
该列如下所示: screenshot of jupyter notebook
答案 0 :(得分:2)
工作示例
df = pd.DataFrame(dict(location=['(hello)']))
print(df)
location
0 (hello)
@ Psidom的解决方案
str.strip
df.location.str.strip('()')
0 hello
Name: location, dtype: object
选项2
str.extract
df.location.str.extract('\((.*)\)', expand=False)
0 hello
Name: location, dtype: object
选项3
str.replace
df.location.str.replace('\(|\)', '')
0 hello
Name: location, dtype: object
选项4
replace
df.location.replace('\(|\)', '', regex=True)
0 hello
Name: location, dtype: object
答案 1 :(得分:0)