Zorp
Zorp

Reputation: 105

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?

Upvotes: 0

Views: 692

Answers (1)

Coderio
Coderio

Reputation: 442

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

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

Upvotes: 1

Related Questions