繁体   English   中英

如果字符串包含特定字符串,则删除字符串和括号。 熊猫

[英]remove string and brackets if string contains specific string. Pandas

我试图弄清楚如果一列包含某个字符串,如何删除字符串和括号。

所以在我的专栏中,我可以有两个包含这样括号的字符串

col a
this is the text(text it is) and other information (COO: warning) some other information

我能做的是删除所有文本和括号。 但是如何删除包含 COO: 的字符串和括号?

cork_ing['DS VALUE Check'] = cork_ing['DS_VALUE'].str.replace(r"\(.*?\)","")

我可以修改上面的代码行来删除和字符串 COO: 吗?

您可以在括号中定位仅包含文本COO术语,例如

cork_ing['DS VALUE Check'] = cork_ing['DS_VALUE'].str.replace(r'\([^)]*COO.*?\)', '')

正则表达式模式说明:

\(     match literal (
[^)]*  then match zero or more non ) characters
COO    match 'COO'
.*?    match remaining content up until the first
\)     literal closing )
cork_ing['DS VALUE Check'] = cork_ing['DS_VALUE'].apply(lambda string: string.replace("(","").replace(")","") if "COO" in string else string)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM