'String as a condition in a filter

In my program, I want the user to be able to pass a string as a condition. For example, if the user input is "col(X) | col(Y)", I would like this string to be the filter condition in the filter function of a Dataframe. So an example will be like this:

condition = "col(X) | col(Y)"
dataFrame.filter(condition)

I saw on the documentation that I need an Expr to pass in the filter, but how can I transform a string to an Expr?



Solution 1:[1]

You can use eval(), builtin-function from Python like so:

condition = "col(X) | col(Y)"
dataFrame.filter(eval(condition))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Naufal Hilmiaji