pandas 从熊猫列中删除字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43768023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
remove characters from pandas column
提问by Keenan Burke-Pitts
I'm trying to simply remove the '(' and ')' from the beginning and end of the pandas column series. This is my best guess so far but it just returns empty strings with () intact.
我试图简单地从 pandas 列系列的开头和结尾删除 '(' 和 ')'。这是我迄今为止最好的猜测,但它只返回 () 完好无损的空字符串。
postings['location'].replace('[^\(.*\)?]','', regex=True)
The column looks like this: screenshot of jupyter notebook
该列如下所示: jupyter notebook 截图
回答by piRSquared
Working example
工作示例
df = pd.DataFrame(dict(location=['(hello)']))
print(df)
location
0 (hello)
@Psidom's Solutionstr.strip
@Psidom 的解决方案str.strip
df.location.str.strip('()')
0 hello
Name: location, dtype: object
Option 2str.extract
选项 2str.extract
df.location.str.extract('\((.*)\)', expand=False)
0 hello
Name: location, dtype: object
Option 3str.replace
选项 3str.replace
df.location.str.replace('\(|\)', '')
0 hello
Name: location, dtype: object
Option 4replace
选项 4replace
df.location.replace('\(|\)', '', regex=True)
0 hello
Name: location, dtype: object
回答by Rahul
What you are doing with [^\(.*\)?]
is match all other characters than you mentioned in character class. ^
inside character class means negating that set.
您正在做的[^\(.*\)?]
是匹配除您在 character class 中提到的所有其他字符。^
在字符类中意味着否定该集合。
Should try with ^\(|\)$
and replace with ""
i.e empty string.
应该尝试使用^\(|\)$
并替换为""
ie 空字符串。