How to use str.contains() with multiple expressions, in pandas dataframes?

前端 未结 2 886
甜味超标
甜味超标 2020-12-05 02:27

I\'m wondering if there is a more efficient way to use the str.contains() function in Pandas, to search for two partial strings at once. I want to search a given column in a

2条回答
  •  广开言路
    2020-12-05 02:51

    The is one regular expression and should be in one string:

    "nt|nv"  # rather than "nt" | " nv"
    f_recs[f_recs['Behavior'].str.contains("nt|nv", na=False)]
    

    Python doesn't let you use the or (|) operator on strings:

    In [1]: "nt" | "nv"
    TypeError: unsupported operand type(s) for |: 'str' and 'str'
    

提交回复
热议问题