Python 3.6下消除DataFrame含OR正则的UserWarning方法问询
str.contains() Regex Group Warning in Pandas Got it, let's work through this warning you're hitting in Pandas.
The warning pops up because you're using a capturing group ((word1|word2)) in your regex with str.contains(). Pandas is basically nudging you: "Capturing groups are meant for extracting specific text chunks (you'd use str.extract() for that), but here you're just checking if a match exists—so those groups aren't serving their intended purpose here."
The good news is you don't have to mess with your regex's core logic to fix this. Just turn that capturing group into a non-capturing group—this keeps your original matching behavior intact (you still match either word1 or word2 as whole words) but tells the regex engine not to store the matched group, which gets rid of the warning.
Here's your updated code:
laDataTemps.loc[laDataTemps['texte'].str.contains(r'\b(?:word1|word2)\b', regex=True)]
The ?: inside the parentheses is the key—it marks the group as non-capturing. Your regex will filter rows exactly like before: it checks for either word1 or word2 surrounded by word boundaries, no changes to which rows get selected.
内容的提问来源于stack exchange,提问作者TmSmth

