Apply dataframe style base on multiselect columns

Context: Iโ€™m trying to apply string and background gradient formatting to a dataframe as such:

                'A': '{:.0%}', 
                'B': '{:.0%}',
                'C': '{:.0%}',

The multi-select only shows โ€œAโ€,โ€œBโ€ and โ€œCโ€ as options:

choices = st.multiselect('Choose the date filters to apply',['A','B','C'])

However, my dataframe is actually multi-index and so I can grab the columns based on the choices from the multiselect doing this:

chosen_cols = [col for col in list(dataframe.columns) for choice in choices if choice in col]

Now, how can I apply the .format dynamically based on the options a user selected? Using apply? How could I do that?

I was trying to do this:

st.dataframe(dataframe[chosen_cols]) and this works if the dataframe doesnโ€™t have style applied to it (based on the options seleted by the user), but if I apply the style it says โ€œStylerโ€ object is not subscriptable (since itโ€™s no longer a dataframe so I canโ€™t subset the columns based on the options chosen)

How could I overcome this issue?


For an overview, the dataframe looks more or like this:


This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.