[英]How do I remove a specific part of a string?
I'd like to remove a specific part of a string within a pandas df.我想删除熊猫 df 中字符串的特定部分。 More precisely, I want the script to remove everything within '(' and ')'.
更准确地说,我希望脚本删除“(”和“)”中的所有内容。 Example:
例子:
'3453(s656)s(657)' -script-> '3453s'
Is there an easy implemented python function or need I to script it by my self?是否有一个简单实现的 python 函数或需要我自己编写脚本? Thanks for any help!
谢谢你的帮助!
You could use str.replace
.你可以使用
str.replace
。 Here's an example dataframe:这是一个示例数据框:
df = pd.DataFrame({'col1':['3453(s656)s(657)', 'another(---)string']})
df['col1'] = df.col1.str.replace(r'(\(.*?\))', '')
col1
0 3453s
1 anotherstring
you could implement a simple python function to do just the following as:您可以实现一个简单的python函数来执行以下操作:
def speclace(a)
x=""
s=0
for i in a:
if i=='(':
s=1
elif i==')':
s=0
elif s==0:
x+=i
return x
<-- Upvote if helpful --> <-- 有帮助就点个赞-->
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.