Suppose a I got a string from front end and such as
str='(A==1) & (B==\'A\') & (C>sin(2))'
this is the simplest format, the string could be much much more complex.
and I would like apply the condition in dataframe filtering, such as
data = {'A': [1, 2, 3, 4],\
'B': ['A','B','C','D'],\
'C':[0.1,0.2,0.3,0.4]}
df=pd.DataFrame(data)
df_test=df[eval(str)]
To make this work, I have to find variables A,B,C
in the string and replace them by df.A, df.B, df.C.
I've tried the following method
import ast
names = [node.id for node in ast.walk(ast.parse(str)) if isinstance(node, ast.Name)]
print(names)
but it returns ['C', 'A', 'B', 'sin']
in which 'sin' is not required.
I also tried pyparse
but still can not figure out how to define the pattern of variable name.
It will be much appreciated if you can help to give me some advice on how to find and replace the variable name in string?