- How do I prevent showing dataframe index when I write it out as a table?
- 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.
Thanks in advance.
Hello @huani!
, 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
.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,
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 !
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.