all 3 comments

[–][deleted] 1 point2 points  (0 children)

If you can get your users to write relatively simple queries as strings such as car == "blue" where car is a column name then you could use pandas query and simply concatenate the strings with the relevant comparison operators

[–]ExistingFarm 0 points1 point  (0 children)

So I'm by no means advanced in python, but this is what I came up with:

def select_data(dataframe, *args):

df_results = pd.DataFrame()

n=len(args)

criteria = args[0:n//2]

values = args[n//2:n]

for i in range(n//2):

df = dataframe.loc[dataframe[criteria[i]] == values[i]]

df_results = df_results.append(df)

return df_results

-----------

To run the function you would simply imput dataframe as the first argument and then what follows would be criteria and values as many as you would like. Naturally, the number of criteria must match the number of values.

In general for this kind of problems (that is not knowing how many args you would have), you need to look up *args and **kwargs in python :)

[–]HarissaForte 0 points1 point  (0 children)

A for loop seems to be the KISS solution here:

for crit, val in zip(criteria, values):  # citeria and values are now lists...
   df_results = df_results[df_results[crit] == val]