1

This is a follow up question from here

What is the best way to include the re flags inside the query.

The following way throws an error

condition = f"(col1.str.contains('{val}', flags={re}.IGNORECASE)"
df.query(condition)

Syntax Error:

....
File "<unknown>", line 1

 col1.str.contains ('val',flags =<module 're'from '/xxxx/lib/python3.7/re.py'>.IGNORECASE )

SyntaxError: invalid syntax
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
Venkatachalam
  • 16,288
  • 9
  • 49
  • 77

2 Answers2

2

Also you could instead use the corresponding inline flags:

df = pd.DataFrame({'col1':list('aaAAbC')})

condition = f"col1.str.contains('(?i)a')" 
print (df.query(condition, engine = 'python'))

Note that (?i) is the inline flag that corresponds to re.IGNORECASE. I tend to believe that re.DEBUG is the only flag that does not contain a corresponding inline flag. check python for the corresponding inline flags

Onyambu
  • 67,392
  • 3
  • 24
  • 53
1

For me working pass variable with @ and add engine="python":

df = pd.DataFrame({'col1':list('aaAAbC')})

a = re.IGNORECASE
condition = f"col1.str.contains('a', flags=@a)"

print (df.query(condition, engine="python"))
  col1
0    a
1    a
2    A
3    A
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252