I have one dataframe that I need to filter based on condition receiving in request from UI. request example:
{
"table": "abc",
"condition": "A=98 and C=73 and D='rendom_char'"
}
dataframe sample:
A | B | C | D | |
---|---|---|---|---|
0 | 85 | 39 | 54 | td |
1 | 39 | 51 | 23 | abc |
2 | 98 | 17 | 73 | def |
3 | 98 | 52 | 73 | def |
4 | 85 | 52 | 21 | rst |
5 | 61 | 89 | 31 | xvz |
so suppose if I get condition from UI that "condition": "A=98 and C=73 and D='def'"
or "condition": "A=98 and C=73"
output should be like :
A | B | C | D | |
---|---|---|---|---|
2 | 98 | 17 | 73 | def |
3 | 98 | 52 | 73 | def |
The issue I'm facing is how to convert that string condition getting from UI into python form so that I can apply filter on datafram.
CodePudding user response:
Use DataFrame.query
with replace =
to double =
:
d = {"condition": "A=98 and C=73 and D='def'"}
print (df.query(d['condition'].replace('=','==')))
A B C D
2 98 17 73 def
3 98 52 73 def
d = {"condition": "A=85 and C=21"}
print (df.query(d['condition'].replace('=','==')))
A B C D
4 85 52 21 rst
CodePudding user response:
Alternatively, you can query pandas dataframe in SQL fashion with pandasql:
import pandasql as psql
...
req = {"table": "abc", "condition": "A=98 and C=73 and D='def'"}
q = f"select * from df where {req['condition']}"
print(psql.sqldf(q, locals()))
A B C D
0 98 17 73 def
1 98 52 73 def