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

前端 未结 2 881
甜味超标
甜味超标 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'
    
    0 讨论(0)
  • 2020-12-05 02:57

    I try this one and it's work:

    df[df['Behavior'].str.contains('nt|nv', na=False)]
    
    0 讨论(0)
提交回复
热议问题