I have a task where I have to filter a Pandas DataFrame based on user specified logical expression. Now, I've seen a module called PyParser or LARK which I would like to use but I cannot seem to figure out how to set them up.
I have several operators like CONTAINS, EQUAL, FUZZY_MATCH etc. Also, I'd like to combine some expressions into more complex ones.
Example expression:
ColumnA CONTAINS [1, 2, 3] AND (ColumnB FUZZY_MATCH 'bla' OR ColumnC EQUAL 45)
As a result, I'd like to have some structured Dict or List with levels of operations in order of how to execute them. So, the desired result for this example expression would be something like:
[['ColumnA', 'CONTAINS', '[1, 2, 3]'], 'AND', [['ColumnB', 'FUZZY_MATCH', 'bla'], OR, ['ColumnC', 'EQUAL', '45']]]
or in form of dict:
{
'EXPR1': {
'col': 'ColumnA',
'oper': 'CONTAINS',
'value': '[1, 2, 3]']
},
'OPERATOR': 'AND',
'EXPR2': {
'EXPR21': {
'col': 'ColumnB',
'oper': 'FUZZY_MATCH',
'value': 'bla'
},
'OPERATOR': OR,
'EXPR22': {
'col': 'ColumnC',
'oper': 'EQUAL',
'value': '45'
}
}
}
Or something like that. If you have some better way of structuring the result, I'm open for suggestions. I'm pretty new to this so I'm fairly certain this can be improved.