2017-05-03 87 views
2

我试图从熊猫列系列的开头和结尾处简单地删除'('和')'。这是迄今为止我最好的猜测,但它只是返回带有()的空字符串。删除熊猫列中的字符

postings['location'].replace('[^\(.*\)?]','', regex=True) 

列如下: screenshot of jupyter notebook

+5

如果你只是想删除从字符串的开头或结尾的字符你不需要正则表达式。 'strip'应该够了。 'postings ['location']。str.strip(“()”)' – Psidom

回答

1

工作实例

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 
+0

谢谢!选项4工作。大多数其他选项只删除了右括号,而不是我的jupyter笔记本中的开口。 –

0

你所用[^\(.*\)?]做的是匹配所有其它字符比你在字符类提及。 ^里面的字符类意味着否定那个集合。

应该尝试^\(|\)$并替换为""即空字符串。

Regex101 Demo