简体   繁体   English

如何在pandas的查询方法中使用re

[英]how to use re inside the query method of pandas

This is a follow up question from here这是这里的后续问题

What is the best way to include the re flags inside the query.在查询中包含 re 标志的最佳方法是什么。

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

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)是对应于 re.IGNORECASE 的内联标志。 I tend to believe that re.DEBUG is the only flag that does not contain a corresponding inline flag.我倾向于相信re.DEBUG是唯一不包含相应内联标志的标志。 check python for the corresponding inline flags检查python以获取相应的内联标志

For me working pass variable with @ and add engine="python" :对我来说,使用@传递变量并添加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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM