Pattern search in pandas.dataframe.query()

pandas query
check if string is in pandas dataframe
pandas query vs loc
pandas query in list
pandas query variable
pandas query not equal
search pandas dataframe
pandas series query

I have the following dataframe:

Index| col1 | col2 |
0    | 1    | a-b-c

1    | 2    | d-e-f

2    | 3    |  g

I want to be able to make queries like:

myvar= 'a'
df.query('@myvar in col2')

But it always work for exact match. Is there any solution for pattern match?



This should work:

df = pd.DataFrame([[1, 'a-b-c'], [2, 'd-e-f'], [3, 'g']], columns=['col1', 'col2'])

myvar = 'a'


   col1   col2
0     1  a-b-c

Python, Searching Algorithms · Sorting Algorithms · Graph Algorithms · Pattern Searching Pandas provide many methods to filter a Data frame and Dataframe.query() is one of them. Before applying the query() method, the spaces in column names have been replaced with '_'. Check out this Author's contributed articles. The query() method uses a slightly modified Python syntax by default. For example, the & and | (bitwise) operators have the precedence of their boolean cousins, and and or. This is syntactically valid Python, however the semantics are different.

i'm afraid you can't do it using .query(). It will compare your @myvar to the whole string - try to set myvar = "g" and re-execute your query - it'll return you a row with index == 2.

AFAIK, regular expressions aren't supported in DataFrame.query as well

Pandas: Select rows that match a string, Micro tutorial: select rows of a Pandas DataFrame that match a (partial) We can also search less strict for all rows where the column 'model'  Note: Dataframe.query () method only works if the column name doesn’t have any empty spaces. So before applying the method, spaces in column names are replaced with ‘_’ Example #1: Single condition filtering In this example, the data is filtered on the basis of single condition.

You can do something like:

myvar = 'a'

pandas.DataFrame.filter, Keep labels from axis for which, label) == True. select columns by regular expression >>> df.filter(regex='e$', axis=1) one three mouse 1 3  pandas.Series.str.contains¶ Series.str.contains (self, pat, case=True, flags=0, na=nan, regex=True) [source] ¶ Test if pattern or regex is contained within a string of a Series or Index. Return boolean Series or Index based on whether a given pattern or regex is contained within a string of a Series or Index. Parameters pat str

pandas.Series.str.extract, A pattern with two groups will return a DataFrame with two columns. Non-​matches will be NaN. >>> s = pd.Series(['a1', 'b2', 'c3']) >>> s.str.extract(r'([ab])(\d)'​) 0 1  Pandas: Select rows that match a string less than 1 minute read Micro tutorial: Select rows of a Pandas DataFrame that match a (partial) string. import pandas as pd #create sample data data = {'model': ['Lisa', 'Lisa 2', 'Macintosh 128K', 'Macintosh 512K'], 'launched': [1983, 1984, 1984, 1984], 'discontinued': [1986, 1985, 1984, 1986]} df = pd.

pandas.Series.str.contains, Test if pattern or regex is contained within a string of a Series or Index. Analogous, but stricter, relying on re.match instead of Series.str.​startswith. Python Pandas : Select Rows in DataFrame by conditions on multiple columns. In this article we will discuss different ways to select rows in DataFrame based on condition on single or multiple columns. Select Rows based on value in column. It will return a DataFrame in which Column ‘Product‘ contains ‘Apples‘ only i.e.

Pandas Select rows by condition and String Operations, Search for String in Pandas Dataframe contains the substring “ville” in it's city name using str.contains() function Search String with Patterns. You can use two main strategies to search for text in strings. Methods of the String class search for specific text. Regular expressions search for patterns in text. The C# examples in this article run in the Try.NET inline code runner and playground. Select the Run button to run an example in an interactive window. Once you execute the code