我试图从熊猫列系列的开头和结尾处简单地删除'('和')'。这是迄今为止我最好的猜测,但它只是返回带有()的空字符串。删除熊猫列中的字符
postings['location'].replace('[^\(.*\)?]','', regex=True)
列如下: screenshot of jupyter notebook
我试图从熊猫列系列的开头和结尾处简单地删除'('和')'。这是迄今为止我最好的猜测,但它只是返回带有()的空字符串。删除熊猫列中的字符
postings['location'].replace('[^\(.*\)?]','', regex=True)
列如下: screenshot of jupyter notebook
工作实例
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
谢谢!选项4工作。大多数其他选项只删除了右括号,而不是我的jupyter笔记本中的开口。 –
如果你只是想删除从字符串的开头或结尾的字符你不需要正则表达式。 'strip'应该够了。 'postings ['location']。str.strip(“()”)' – Psidom