簡體   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