logologo
  • AI Tools

    DB Query GeneratorMock InterviewResume BuilderLearning Path GeneratorCheatsheet GeneratorAgentic Prompt GeneratorCompany ResearchCover Letter Generator
  • XpertoAI
  • MVP Ready
  • Resources

    CertificationsTopicsExpertsCollectionsArticlesQuestionsVideosJobs
logologo

Elevate Your Coding with our comprehensive articles and niche collections.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Xperto-AI
  • Certifications
  • Python
  • GenAI
  • Machine Learning

Interviews

  • DSA
  • System Design
  • Design Patterns
  • Frontend System Design
  • ReactJS

Procodebase © 2024. All rights reserved.

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Harnessing Streamlit for Dynamic DataFrames and Tables in Python

author
Generated by
ProCodebase AI

15/11/2024

python

Sign in to read full article

Introduction to Streamlit for Data Display

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.

Basic DataFrame Display

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.

Customizing DataFrame Appearance

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.

Using st.table() for Static Tables

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.

Advanced DataFrame Interactions

Streamlit offers more advanced features for working with DataFrames. Let's explore a few:

Editable DataFrames

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.

Conditional Formatting

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.

Column Configuration

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.

Pagination for Large DataFrames

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.

Conclusion

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.

Popular Tags

pythonstreamlitdataframes

Share now!

Like & Bookmark!

Related Collections

  • FastAPI Mastery: From Zero to Hero

    15/10/2024 | Python

  • LangChain Mastery: From Basics to Advanced

    26/10/2024 | Python

  • Mastering NumPy: From Basics to Advanced

    25/09/2024 | Python

  • Python Advanced Mastery: Beyond the Basics

    13/01/2025 | Python

  • Automate Everything with Python: A Complete Guide

    08/12/2024 | Python

Related Articles

  • Exploring Hugging Face Model Hub and Community

    14/11/2024 | Python

  • Getting Started with Matplotlib

    05/10/2024 | Python

  • Bar Charts and Histograms Explained

    05/10/2024 | Python

  • Seaborn Fundamentals

    06/10/2024 | Python

  • Advanced Pattern Design and Best Practices in LangChain

    26/10/2024 | Python

  • Mastering FastAPI Testing

    15/10/2024 | Python

  • Customizing Line Plots in Matplotlib

    05/10/2024 | Python

Popular Category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design