kalise
kalise

Reputation: 13

Code logic for a graphical interface based on pandas dataframe columns

I am looking for a good way to create a custom graphical interface that allows for a condition-based evaluation of a pandas dataframe.

For example, a user might want to customize the following condition df.A > df.B to df.A < df.B. In other instances, they may refer to different column names.

This is easy with a command-line interface, but how can I incorporate this into a graphical tool?

For the above example, I am thinking of a pseudocode similar to this:

if text = 'df.A > df.B':
    df['result'] = df.A > df.B
elif text = 'df.B > df.A':
    df['result'] = df.B > df.A

#and so on

FYI, this will be a web app.

Thank you.

Upvotes: 0

Views: 127

Answers (1)

Dmitriy Nikolaev
Dmitriy Nikolaev

Reputation: 56

If you trust your users input, use DataFrame.query https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.query.html

# text = 'A > B'
df['result'] = df.query(text)

If you do not trust your users, you have to sanitize their input first

Upvotes: 1

Related Questions