Streamlit is a powerful Python library that makes it incredibly easy to create interactive web applications for data science and machine learning projects. One of its standout features is the ability to display and manipulate DataFrames and tables with minimal code. In this blog post, we'll explore how to leverage Streamlit to present your data effectively.
Let's start with the basics. Displaying a DataFrame in Streamlit is as simple as using the st.dataframe()
function. Here's a quick example:
import streamlit as st import pandas as pd # Create a sample DataFrame df = pd.DataFrame({ 'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'City': ['New York', 'San Francisco', 'London'] }) # Display the DataFrame st.dataframe(df)
This code will render a simple, interactive table in your Streamlit app. Users can sort columns and scroll if the DataFrame is large.
Streamlit allows you to customize the appearance of your DataFrames. You can set the width and height, highlight specific values, and more. Here's an example:
st.dataframe(df.style.highlight_max(axis=0), width=700, height=300)
This will display the DataFrame with the maximum value in each column highlighted, and set a specific width and height for the table.
While st.dataframe()
creates an interactive table, sometimes you might want a static view. That's where st.table()
comes in handy:
st.table(df)
This creates a static table that fits the width of the container and doesn't have interactive features like sorting or scrolling.
Streamlit offers more advanced features for working with DataFrames. Let's explore a few:
You can create editable DataFrames using the st.data_editor()
function:
edited_df = st.data_editor(df)
This allows users to modify the data directly in the app. The edited DataFrame is returned, which you can then use for further processing.
You can apply conditional formatting to highlight specific data points:
st.dataframe(df.style.applymap(lambda x: 'color: red' if x > 30 else 'color: black', subset=['Age']))
This will color any age value over 30 in red.
Streamlit allows you to configure individual columns in your DataFrame display:
st.dataframe(df, column_config={ "Name": st.column_config.TextColumn( "Full Name", help="The person's full name", max_chars=50, ), "Age": st.column_config.NumberColumn( "Age (years)", help="The person's age", min_value=0, max_value=120, step=1, ), "City": st.column_config.SelectboxColumn( "Current City", help="The city where the person lives", width="medium", options=[ "New York", "San Francisco", "London", "Tokyo", "Berlin" ], ) })
This configuration adds help text, sets input types, and provides a dropdown for the City column.
When dealing with large DataFrames, pagination can improve performance and user experience:
import math # Assume 'large_df' is your large DataFrame items_per_page = 10 pages = math.ceil(len(large_df) / items_per_page) page = st.selectbox('Page', range(1, pages + 1)) start_idx = (page - 1) * items_per_page end_idx = start_idx + items_per_page st.dataframe(large_df.iloc[start_idx:end_idx])
This code creates a paginated view of your large DataFrame, allowing users to navigate through pages easily.
Streamlit provides a rich set of tools for working with DataFrames and tables in Python. From basic displays to advanced interactions, you can create engaging and informative data presentations with just a few lines of code. As you continue to explore Streamlit, you'll discover even more ways to enhance your data visualization and create powerful, interactive web applications.
05/11/2024 | Python
08/11/2024 | Python
15/10/2024 | Python
22/11/2024 | Python
25/09/2024 | Python
25/09/2024 | Python
17/11/2024 | Python
15/10/2024 | Python
15/11/2024 | Python
05/11/2024 | Python