【发布时间】:2019-08-30 10:35:31
【问题描述】:
我想使用re 和contain:
使用| 由多个模式形成的模式,但我也想使用“但不是”。
我想对工作类型进行分类。
#Data:
v=pd.Series(['New wiring system for an extra room','Build a wall and add a new door',
'Fix a shelving unit'])
v=v.str.lower()
print(v)
#I construct this pattern:
pattern_cons='ret|wall|ceiling|buil|holes|cons'
pattern_nrg= 'wiring|media|elect'
pattern_plumb='water'
pattern_carp= 'shelving|table|door'
pattern_work=pd.Series([pattern_cons,pattern_nrg,pattern_plumb,
pattern_carp])
# Use this code : (I loop this)
for x in range(4):
pattern=pattern_work
vector={'pattern': pattern_work[x],'type_work':class_str[x]}
print(vector)
s=v.str.contains(vector['pattern'], flags=re.IGNORECASE, regex=True)
print(s)
我得到这个输出:
0 new wiring system for an extra room
1 build a wall and add a new door
2 fix a shelving unit
dtype: object
{'pattern': 'ret|wall|ceiling|buil|holes|cons', 'type_work': 'cons'}
0 False
1 True
2 False
dtype: bool
{'pattern': 'wiring|media|elect', 'type_work': 'nrg'}
0 True
1 False
2 False
dtype: bool
{'pattern': 'water', 'type_work': 'plumb'}
0 False
1 False
2 False
dtype: bool
{'pattern': 'shelving|table|door', 'type_work': 'carp'}
0 False
1 True # ------- **** I WANT THIS "False" **** ------- #
2 True
dtype: bool
问题是最后一个字符串被分配了2个类。
'Build a wall and add a new door' 被归类为 cons 类和 carp 类。
但我希望字符串为 False 对应 pattern_carp。
是否可以使用排除?!buil 的模式。我的意思是这样的? :
`pattern_carp= 'shelving|table|door(?!buil'`
【问题讨论】: