Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Reg-Ex匹配数据帧中的字符串并替换-python_Python_Regex_Python 3.x_Pandas_Strip - Fatal编程技术网

使用Reg-Ex匹配数据帧中的字符串并替换-python

使用Reg-Ex匹配数据帧中的字符串并替换-python,python,regex,python-3.x,pandas,strip,Python,Regex,Python 3.x,Pandas,Strip,我有这样的数据框 value 0 A067-M4FL-CAA-020 1 MRF2-050A-TFC,60 ,R-12,HT 2 moreinfo 3 MZF8-050Z-AAB 4 GoCats 5 MZA2-0580-TFD,60 ,R-669,LT 我希望能够使用正则表达式剥离、60、R-12、HT,并从df中删除moreinfo和GoCats行 我的预期结果: value 0 A067-M4FL-CAA-020 1 MRF2-050A-

我有这样的数据框

    value
0   A067-M4FL-CAA-020
1   MRF2-050A-TFC,60 ,R-12,HT
2   moreinfo
3   MZF8-050Z-AAB
4   GoCats
5   MZA2-0580-TFD,60 ,R-669,LT
我希望能够使用正则表达式剥离
、60、R-12、HT
,并从df中删除
moreinfo
GoCats

我的预期结果:

     value
0   A067-M4FL-CAA-020
1   MRF2-050A-TFC
2   MZF8-050Z-AAB
3   MZA2-0580-TFD
我先去掉了线

del = ['hello', 'moreinfo']
for i in del:
   df = df[value!= i]
有人能建议一种使用正则表达式匹配和删除所有匹配的案例的方法吗
A067-M4FL-CAA-020
MZF8-050Z-AAB
模式,这样我就不必为所有可能的案例创建列表了

我可以像这样剥离一行,但我希望能够剥离数据帧中所有匹配的案例

pattern = r',\w+ \,\w+-\w+\,\w+ *'
line = 'MRF2-050A-TFC,60 ,R-12,HT'
for i in re.findall(pattern, line):
   line = line.replace(i,'')

>>> MRF2-050A-TFC
我试图调整代码,但它为每一行打印出相同的输出

pattern = r',\w+ \,\w+-\w+\,\w+ *'
for d in df:
   for i in re.findall(pattern, d):
     d = d.replace(i,'')

如有任何建议,将不胜感激。谢谢

我建议捕获您想要的数据,因为它非常特殊,您不想要的数据可能是任何东西

您的模式应该如下所示:

^\w{4}-\w{4}-\w{3}(?:-\d{3})?

我建议尽可能地比
\w
更具体一些。(如
^[A-Z]\w{3}
)如果您知道开头的四个字符块应该以字母开头

编辑 很抱歉,我可能没有足够准确地阅读您的输入和输出:

你可以试试这个

(?:\w+-){2,}[^,\n]*

Python脚本可能如下所示

ss="""0   A067-M4FL-CAA-020
1   MRF2-050A-TFC,60 ,R-12,HT
2   moreinfo
3   MZF8-050Z-AAB
4   GoCats
5   MZA2-0580-TFD,60 ,R-669,LT"""

import re
regx=re.compile(r'(?:\w+-){2,}[^,\n]*')
m= regx.findall(ss)

for i in range(len(m)):
    print("%d   %s" %(i, m[i]))
输出是

0   A067-M4FL-CAA-020
1   MRF2-050A-TFC
2   MZF8-050Z-AAB
3   MZA2-0580-TFD

这里有一个更简单的方法,您可以不使用正则表达式来尝试。pandas有许多内置函数来处理文本数据

# remove unwanted values
df['value'] = df.value.str.replace(r'moreinfo|60|R-.*|HT|GoCats|\,', '')

# drop na
df = df[(df != '')].dropna()

# print
print(df)

    value
0   A067-M4FL-CAA-020
1   MRF2-050A-TFC
3   MZF8-050Z-AAB
5   MZA2-0580-TFD

-----------
# data used
df = pd.read_fwf(StringIO(u'''
    value
0   A067-M4FL-CAA-020
1   MRF2-050A-TFC,60 ,R-12,HT
2   moreinfo
3   MZF8-050Z-AAB
4   GoCats
5   MZA2-0580-TFD,60 ,R-669,LT'''),header=1)

df['value']=df['value'].str.replace(r)^([A-Z0-9]+(?:-[A-Z0-9]+){2,})。*^((:moreinfo | GoCats)$”,r“\1”)
,然后
df[df['value'!=”
# remove unwanted values
df['value'] = df.value.str.replace(r'moreinfo|60|R-.*|HT|GoCats|\,', '')

# drop na
df = df[(df != '')].dropna()

# print
print(df)

    value
0   A067-M4FL-CAA-020
1   MRF2-050A-TFC
3   MZF8-050Z-AAB
5   MZA2-0580-TFD

-----------
# data used
df = pd.read_fwf(StringIO(u'''
    value
0   A067-M4FL-CAA-020
1   MRF2-050A-TFC,60 ,R-12,HT
2   moreinfo
3   MZF8-050Z-AAB
4   GoCats
5   MZA2-0580-TFD,60 ,R-669,LT'''),header=1)