How to dynamically calculate/aggregate using different variables (like a pivot table)

Just started with streamlit and I’m really enjoying it. I created a simple multiselect widget that lets me choose multiple variables from a dataframe(variable1, variable2, variable3…variableN).
Suppose I have a simple pandas dataframe that I want to do a simple aggregation. For example, Sum Variable1 and be able to group by either Variable2 or Variable3. How do I do it such that I can swap var2 for var3 in the multiselect dropdown and have it dynamically change, much like how I would do in an excel pivot table? Can I do it all in one multiselect widget or do I need to have a seperate “Y” and a seperate “X”? Not sure if the multiselect tool widget is the best one for this or if there is another widget for this purpose?