在python中使用正则表达式根据某些模式删除一些行

时间:2018-06-25 19:53:23

标签: python regex

我有一个数据表,其中有一些文本列。我想删除那些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

1 个答案:

答案 0 :(得分:0)

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))