Author(s): Jose Parreño
Learn how to easily style your dataframes in Streamlit using the Pandas Styler object. Despite some initial incompatibility, we’ll show you how to make them work together seamlessly.
Disclaimer: This post has been created automatically using generative AI. Including DALL-E, Gemini, OpenAI and others. Please take its contents with a grain of salt. For feedback on how we can improve, please email us
Introduction
Dataframes are a crucial part of data analysis and visualization. They allow us to organize and manipulate data in a structured and efficient manner. However, creating well-styled dataframes can be a challenging task, especially when working with large datasets. In this blog post, we will explore how to use the Pandas Styler and Streamlit to create visually appealing and organized dataframes.
The Pandas Styler Object
The Pandas Styler object is a powerful tool that allows us to customize the appearance of our dataframes. It provides several methods for styling data, such as applying colors, fonts, and conditional formatting. However, the Styler object is not compatible with Streamlit by default. This can be frustrating for users who want to display their styled dataframes in a Streamlit app. But, fear not, we will show you how to overcome this obstacle in the following sections.
Setting Up Streamlit
Before we dive into using the Pandas Styler, we need to set up our Streamlit environment. Streamlit is an open-source framework that allows us to build interactive web applications with ease. To install Streamlit, we can use pip or conda, depending on our preference. Once installed, we can create a new Python file and import the necessary libraries, including pandas and Streamlit.
Using the Pandas Styler with Streamlit
The good news is that there is a simple workaround to use the Pandas Styler with Streamlit. We can create a custom function that takes in a dataframe and applies our desired styling using the Styler object. Then, we can use the Streamlit function “st.dataframe()” to display our styled dataframe in our app. This method allows us to use all the styling capabilities of the Pandas Styler while still being able to display our data in Streamlit.
Adding Interactivity to our Dataframes
One of the great features of Streamlit is its ability to add interactivity to our apps. We can use this feature to make our dataframes even more user-friendly and visually appealing. For example, we can add a slider to our app that allows users to change the font size of our dataframe. We can also add a dropdown menu that lets users select which columns they want to display in the dataframe. These interactive elements make our dataframes more dynamic and customizable.
Conclusion
In conclusion, while the Pandas Styler and Streamlit may not have been the best of friends in the past, this article has shown us how we can change that. By using the Pandas Styler object and Streamlit together, we can create well-styled dataframes that are both visually appealing and informative. With the tips and techniques shared in this article, you can now confidently create stylish dataframes in your Streamlit apps. So, go ahead and give it a try!
Crafted using generative AI from insights found on Towards Data Science.
Join us on this incredible generative AI journey and be a part of the revolution. Stay tuned for updates and insights on generative AI by following us on X or LinkedIn.