简体   繁体   中英

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: ??

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: ?

You could target terms in parentheses which only also contain the text COO , eg

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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