Since I haven’t received any response for almost 2 days now, I guess this is a feature that is not yet supported in streamlit.
I found a workaround that could do the trick, although it’s not really what I wanted. It consists of changing the pivot_table back into a regular dataframe.
Code snippet
import streamlit as st
import pandas as pd
import calendar
import sys
def read_csv(PATH: str) -> pd.DataFrame:
try:
df = pd.read_csv(PATH)
except:
sys.exit('Unable to read the data, kindly verify the source and try again')
abbr = dict(enumerate(calendar.month_abbr))
abbr.pop(0)
df['MONTH'] = pd.Categorical(
df['MONTH'], categories=list(abbr.values()), ordered=True)
return df
with st.sidebar:
st.info('This is my first web application with streamlit')
PATH = 'https://raw.githubusercontent.com/Lamy237/hello-world/main/pay.csv'
df = read_csv(PATH)
monthly_pay_df = df.pivot_table(values='PAY', index='MONTH', columns='YEAR')
# changing back to regular df
monthly_pay_df = monthly_pay_df.reset_index().rename_axis(None, axis='columns')
st.dataframe(monthly_pay_df, use_container_width=True)
I wanted to share that you might have better luck implementing a pivot table with the streamlit-aggrid component – in fact, we have a blog post written by Pablo Fonseca, the creator of the streamlit-aggrid component, about building a pivot table with streamlit-aggrid.
hello @rimano , In you above image I see that the pivot table has an extra roe with Month under Year how did you display the data frame with all in a single row. by removing year. Please advice. Thanks.
The answer to your question is I didn’t. In fact, that is what this topic is all about (Guess you weren’t very attentive there ).
This is a default streamlit behaviour and I have been trying to find a workaround. However, if that’s the behaviour you are expecting the code snippet I provided before can help you make it look a bit better. Otherwise, you may look into the solution proposed by @Caroline.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
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.
Performance cookies
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.
Functional cookies
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.
Targeting cookies
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.