Pandas and MySQL with explanation
Pandas\npy []\nimport pandas as pd\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n    mask = patients[\'conditions\'].str.contains(r\'\\bDIAB1\'
## Pandas\n```py []\nimport pandas as pd\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n    mask = patients[\'conditions\'].str.contains(r\'\\bDIAB1\')\n    return patients[mask]\n```\nWithout r and adding regex=True doesn\'t work -- (\'\\bDIAB1\', regex=True)\nBing explained :\n------------------\nNo, the `r` prefix does not mean regex. It means raw string, which is a way of writing strings in Python that do not interpret escape sequences. The `r` prefix does not affect how str.contains() interprets the string, it only affects how Python interprets the string. A raw string is a way of telling Python to ignore the escape sequences and treat them as literal characters. This does not mean that str.contains() will also ignore them and treat them as literal characters. The str.contains() method will still read them as special characters and use them to match the pattern in the column.\n\nYou can think of it like this: Python reads the raw string and passes it to str.contains() without changing anything. Then, str.contains() reads the string and applies the rules of regular expressions to find the matches in the column.\n\n`\\b` is a backspace when it is used in a normal string, but it is a word boundary when it is used in a regular expression. The str.contains() method interprets the regular expression according to the rules of the Python re module where `\\b` means `word boundary` not backspace..\n\nIf you use `str.contains(\'\\bDIAB1\') as a normal string`, Python will interpret the `\\ b` as a special character that means a backspace. This means that it will erase the previous character in the string. For example, \'cat\\b\' will become \'ca\'. This is not what you want when you are looking for the condition \u2018DIAB1\u2019 as a FRONT PART OF A WORD. You want to use the `\\b` as a metacharacter that means `a word boundary`..\n\nSUMMARY : Let Python read `\'\\bDIAB1\'` as a raw string(`r\'\\bDIAB1\'`) then let str.contains( ) read it as regular expression i.e. regex.\n\n## A easy way, use \' \\ \' before \'\\b\' to work \'\\b\' as a boundary word because \n- `\\b` in a normal string means \'backspace\' but in regular expression it\'s a `boundary word` and we want `\\b` to work as a `boundary word` NOT AS A `backspace`, so use `\\` before `\\b` (`\\\\b`) to work it as a special character in regular expression. (This same theory works for MySQL as well)\n  \n                                \'\\\\bDIAB1\'\n\n```py []\nimport pandas as pd\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n    mask = patients[\'conditions\'].str.contains(\'\\\\bDIAB1\')\n    return patients[mask]\n```\n\n## MySQL\n```sql []\nSELECT * \nFROM patients\nWHERE conditions REGEXP \'\\\\bDIAB1\'\n```\n\\b won\'t work as a boundary word if you don\'t put a `\\` before `\\b` in MySQL.\n\n### If the post was helpful, an upvote will really be appreciated. Thank Youu
['MySQL', 'Pandas']