[英]remove string and brackets if string contains specific string. Pandas
I'm trying to figure out how to remove string and brackets if a column have contains a certain string.我试图弄清楚如果一列包含某个字符串,如何删除字符串和括号。
So in my column I can have two strings that contain brackets like this所以在我的专栏中,我可以有两个包含这样括号的字符串
col a
this is the text(text it is) and other information (COO: warning) some other information
What I can do is remove all text and brackets.我能做的是删除所有文本和括号。 But how do I just remove the string and brackets that contains COO: ??但是如何删除包含 COO: 的字符串和括号?
cork_ing['DS VALUE Check'] = cork_ing['DS_VALUE'].str.replace(r"\(.*?\)","")
Can I modify the above code line just to remove and string with COO: ?我可以修改上面的代码行来删除和字符串 COO: 吗?
You could target terms in parentheses which only also contain the text COO
, eg您可以在括号中定位仅包含文本COO
术语,例如
cork_ing['DS VALUE Check'] = cork_ing['DS_VALUE'].str.replace(r'\([^)]*COO.*?\)', '')
Explanation of regex pattern:正则表达式模式说明:
\( 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.