Questions on st.table

  1. How do I prevent showing dataframe index when I write it out as a table?
  2. Even after rounding in the dataframe, when I write out the table, it is still showing something like 11.5000 for some cells. How do I get rid of those?

Thanks in advance.

Hello @huani!

  1. Hmmmmโ€ฆunfortunately I donโ€™t think this is possible :frowning:, this bit is hardcoded in the Streamlit codebase, your best bet would be to whip out your own Table component. Feel free to open an issue on Github by specifying why your use case requires to remove the index column :slight_smile: .
  2. I think this is covered in this thread and by the Pandas Styler, the rounding precision of the dataframe displayed in the browser (managed through Styler) can be different from the rounding precision on the Python side. Are you looking for something like :
import streamlit as st
import pandas as pd
import numpy as np

df = pd.DataFrame(
    np.random.randn(50, 20),
    columns=('col %d' % i for i in range(20))
)

st.table(df.style.set_precision(2))

?

Best,
Fanilo

Hi,
Adding on @andfaniloโ€™s answer,

For first, you could try two of these ugly hacks.

import pandas as pd
import streamlit as st

df = pd.DataFrame({"x": [1, 2, 3, 4], "y": list("abcd")})

# set first td and first th of every table to not display
st.markdown("""
<style>
table td:nth-child(1) {
    display: none
}
table th:nth-child(1) {
    display: none
}
</style>
""", unsafe_allow_html=True)

st.table(df)

or

import pandas as pd
import streamlit as st

df = pd.DataFrame({"x": [1, 2, 3, 4], "y": list("abcd")})

# set index to empty strings

df.index = [""] * len(df)
st.table(df)

Both approaches have their drawbacks,

  1. It will hide first column (index) of every single table
  2. It requires changing df index values. It will show a column with no values instead of hiding the column actually.

For the second one You can try printing as type โ€œstrโ€, ( again just a hack )

import pandas as pd
import streamlit as st

df = pd.DataFrame({"x": [1.0, 2.13333333, 3.3, 4.2], "y": list("abcd")})

st.write(df.round(2).astype("str"))

Hope it helps !

1 Like

Thank you so much @ash2shukla and @andfanilo. Both solutions work beautifully.

One additional question: when we display dataframe, which allows sorting. Is there a way enable filtering, something like Dash DataTable? If not, I really wish someone may have started a component for it.