Filtering DataFrame by finding exact word (not combined) in a column of strings

后端 未结 1 1103
无人及你
无人及你 2020-12-19 18:06

My DataFrame has two columns:

Name  Status
 a    I am Good
 b    Goodness!!!
 c    Good is what i feel
 d    Not Good-at-all

I want to filt

相关标签:
1条回答
  • 2020-12-19 18:45

    If you're defining "exact" to mean no other characters (including punctuation which defines a word boundary \b), you could instead check for a leading and trailing space and/or beginning/end anchors:

    >>> df[df['Status'].str.contains(r'(?:\s|^)Good(?:\s|$)')]
      Name               Status
    0    a            I am Good
    2    c  Good is what i feel
    

    Explanation:

    • (?:\s|^) is a non-capturing group looking for a space character (\s) or the beginning of the string (^).

    • Good is the word you're looking for.

    • (?:\s|$) is a non-capturing group looking for a space character (\s) or the end of the string ($).

    0 讨论(0)
提交回复
热议问题