- 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!
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.
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.