Center cell values from a dataframe dynamically

I’d like to center the cell values of a dataframe, however, I’d like to do it dynamically.

I can kinda center the values like this:


arrays = [
    ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
    ["one", "two", "one", "two", "one", "two", "one", "two"],
]
tuples = list(zip(*arrays))

index = pd.MultiIndex.from_tuples(tuples, names=[None, "Brand"])

df = pd.DataFrame(np.random.randn(3, 8), index=["A", "B", "C"], columns=index)

st.dataframe(df.style.format("{:.0f}".center(30)))

To get this:

But if I stretch the column it no longer stays centered:

So how can I center the values dynamically? And how can I also center the indices (for the headers/columns as multi-index as well)?

Thanks

Hi @roo,

Thanks for flagging this! This seems like it might be an issue with the styled dataframe responsiveness – feel free to submit a GitHub Issue here so our team can look into it further.

Caroline

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