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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:31:43  来源:igfitidea点击:

remove characters from pandas column

pythonregexpandas

提问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 Solution
str.strip

@Psidom 的解决方案
str.strip

df.location.str.strip('()')

0    hello
Name: location, dtype: object

Option 2
str.extract

选项 2
str.extract

df.location.str.extract('\((.*)\)', expand=False)

0    hello
Name: location, dtype: object

Option 3
str.replace

选项 3
str.replace

df.location.str.replace('\(|\)', '')

0    hello
Name: location, dtype: object

Option 4
replace

选项 4
replace

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 空字符串。

Regex101 Demo

Regex101 演示