I have a dataframe that looks like this: foo = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6], 'c': [6,7,8]}) and a list of columns list_of_columns = ['a','b'] of foo.
The list_of_columns are dynamically selected by the user, so it can be ['a','b'] but it can also be ['a','c'] or ['c'] or ['a','b','c'] etc
I would like for every column in the list_of_columns to create (nested) for loops and query the dataframe in the following way:
In case list_of_columns = ['a','b'] the the loop would be like this:
for a in foo.a.unique():
for b in foo.b.unique():
print(foo.query(f'a=={a} and b=={b}'))
In case list_of_columns = ['a'] the the loop would be like this:
for a in foo.a.unique():
print(foo.query(f'a=={a}'))
In case list_of_columns = ['a','b','c'] the the loop would be like this:
for a in foo.a.unique():
for b in foo.b.unique():
for c in foo.c.unique():
print(foo.query(f'a=={a} and b=={b} and c=={c}'))
Is there a way to programmatically achieve that in python ?