They should be 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'
Answer from Andy Hayden on Stack OverflowPandas
pandas.pydata.org › docs › reference › api › pandas.Series.str.contains.html
pandas.Series.str.contains — pandas 3.0.3 documentation
>>> s1.str.contains("house|dog", regex=True) 0 False 1 True 2 True 3 False 4 False dtype: bool
Top answer 1 of 3
81
They should be 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'
2 of 3
0
If you have the patterns in a list, then it might be convenient if you join them by a pipe (|) and pass it to str.contains. Return False for NaNs by na=False and turn off case sensitivity by case=False.
lst = ['nt', 'nv', 'nf']
df['Behavior'].str.contains('|'.join(lst), na=False)
Otherwise, it might be cleaner to group the alternations. For the example in the OP, that is:
df['Behavior'].str.contains(r'n[t|v|f]')
Videos
03:57
String Contains and REGEX | Pandas Tutorial 30.0 - YouTube
01:14
pandas str.contains() gives wrong results? - YouTube
14:20
Filtering Pandas Dataframe with .str.contains, .str.startswith, ...
00:24
Filter DataFrame where it contains a specific string #python #pandas ...
Pandas tutorial | How to filter multiple substring using pandas ...
Pandas str.contains - Search for multiple values in a string and ...
w3resource
w3resource.com › pandas › series › series-str-contains.php
Pandas Series: str.contains() function - w3resource
September 15, 2022 - Pandas Series - str.contains() function: The str.contains() function is used to test if pattern or regex is contained within a string of a Series or Index.
W3Schools
w3schools.com › python › ref_string_endswith.asp
Python String endswith() Method
The endswith() method returns True if the string ends with the specified value, otherwise False.
Python documentation
docs.python.org › 3 › tutorial › datastructures.html
5. Data Structures — Python 3.14.5 documentation
Dictionaries are sometimes found in other languages as “associative memories” or “associative arrays”. Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys. Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key.
Pandas
pandas.pydata.org › pandas-docs › version › 0.18.0 › generated › pandas.Series.str.contains.html
pandas.Series.str.contains — pandas 0.18.0 documentation
Series.str.contains(pat, case=True, flags=0, na=nan, regex=True)¶
Statology
statology.org › home › pandas: check if string contains multiple substrings
Pandas: Check if String Contains Multiple Substrings
October 10, 2022 - df['string_column'].str.contains(r'^(?=.*string1)(?=.*string2)') The following examples show how to use each method in practice with the following pandas DataFrame:
pandas
pandas.pydata.org › Pandas_Cheat_Sheet.pdf pdf
with pandas Cheat Sheet http://pandas.pydata.org
Logic in Python (and pandas) < Less than · != Not equal to · > Greater than · df.column.isin(values) Group membership · == Equals · pd.isnull(obj) Is NaN · <= Less than or equals · pd.notnull(obj) Is not NaN · >= Greater than or equals · &,|,~,^,df.any(),df.all() Logical and, or, not, xor, any, all · regex (Regular Expressions) Examples · '\.' Matches strings containing a period '.' 'Length$' Matches strings ending with word 'Length' '^Sepal' Matches strings beginning with the word 'Sepal' '^x[1-5]$' Matches strings beginning with 'x' and ending with 1,2,3,4,5 ·
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.str.match.html
pandas.Series.str.match — pandas 3.0.3 documentation
For the nullable StringDtype, pandas.NA is used. Returns: Series/Index/array of boolean values · A Series, Index, or array of boolean values indicating whether the start of each string matches the pattern. The result will be of the same type as the input. See also · fullmatch · Stricter matching that requires the entire string to match. contains ·
PYnative
pynative.com › home › python exercises
Python Exercises, Practice, Challenges [570+ Exercises] – PYnative
Topics: Python Basics, Variables, Operators, Loops, String, Numbers, List ... This exercise contains coding challenges to solve using if-else conditions, for loops, the range() function, and while loops.
PyPI
pypi.org › project › python-dotenv
python-dotenv · PyPI
» pip install python-dotenv