【问题标题】:Pandas filtering for multiple patterns and multiple substrings in series using logic | and "but not"Pandas 使用逻辑过滤多个模式和多个子串和“但不是”
【发布时间】:2019-08-30 10:35:31
【问题描述】:

我想使用recontain: 使用| 由多个模式形成的模式,但我也想使用“但不是”。

我想对工作类型进行分类。

#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'`

【问题讨论】:

    标签: pandas contains


    【解决方案1】:

    嗯,我找到了解决它的方法,虽然可能不是最优雅的。 您将如何改进?,事实上,我正在丢失信息。

    我已经添加了这个:

    v=v.replace**("Build a wall and add a new door", "Build a wall and add a new")

    所以现在的代码是:

    v=pd.Series(['New wiring system for an extra room','Build a wall and add a new door',
        'Fix a shelving unit'])
    v=v.replace("Build a wall and add a new door", "Build a wall and add a new")
    print(v.replace("Build a wall and add a new door", "Build a wall and add a new"))
    v=v.str.lower()
    print(v)
    
    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
    2                    Fix a shelving unit
    dtype: object
    0    new wiring system for an extra room
    1             build a wall and add a new
    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    False
    2     True
    dtype: bool
    

    【讨论】:

      猜你喜欢
      • 2018-07-10
      • 1970-01-01
      • 2018-03-10
      • 2017-11-29
      • 1970-01-01
      • 2014-06-23
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      相关资源
      最近更新 更多