簡體   English   中英

在python中使用正則表達式根據某些模式刪除一些行

[英]Use regex in python to remove some rows based on some pattern

我有一些文本列的數據表。 我想刪除那些MN后跟一些數字的行。 例如MN 894080/901060/905034,MN 90706等。

import pandas as pd
data= [
"MN 894080/901060/905034 - a file has some text.",
"L2 BLOCK AMER] [VVol MN 941737][DU MN 934010] a file has some text",
"MN 907068 || bdheks;",
"MN#287627/901060/905034 a file has some text ",
"MN# 944179 || a file has some text",
"(MN #927427)a file has some text",
"MN 933281 - a file has some text",
"a file has some text",
" a file has some text Mnuq"]
df<-pd.DataFrame(data)

最終輸出應如下所示:

df
  data
a file has some text
a file has some text Mnuq
import pandas as pd
data= [
"MN 894080/901060/905034 - a file has some text.",
"L2 BLOCK AMER] [VVol MN 941737][DU MN 934010] a file has some text",
"MN 907068 || bdheks;",
"MN#287627/901060/905034 a file has some text ",
"MN# 944179 || a file has some text",
"(MN #927427)a file has some text",
"MN 933281 - a file has some text",
"a file has some text",
" a file has some text Mnuq"]

_re_remove = re.compile('MN.*\d+')
df = pd.DataFrame(row for row in data if not _re_remove.search(row))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM