logologo
  • AI Interviewer
  • Features
  • Jobs
  • AI Tools
logologo

Transform your hiring process with AI-powered interviews. Screen candidates faster and make better hiring decisions.

Useful Links

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

Resources

  • Certifications
  • Topics
  • Collections
  • Articles
  • Services

AI Tools

  • AI Interviewer
  • Xperto AI
  • AI Pre-Screening

Procodebase © 2025. 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

  • Mastering LangGraph: Stateful, Orchestration Framework

    17/11/2024 | Python

  • Python with Redis Cache

    08/11/2024 | Python

  • Python Advanced Mastery: Beyond the Basics

    13/01/2025 | Python

  • TensorFlow Mastery: From Foundations to Frontiers

    06/10/2024 | Python

  • Automate Everything with Python: A Complete Guide

    08/12/2024 | Python

Related Articles

  • Unleashing the Power of Pandas

    25/09/2024 | Python

  • Mastering Document Loaders and Text Splitting in LangChain

    26/10/2024 | Python

  • Unlocking the Power of Vector Stores and Embeddings in LangChain with Python

    26/10/2024 | Python

  • Unlocking the Power of Visualization in LangGraph for Python

    17/11/2024 | Python

  • Unleashing the Power of Transformers for NLP Tasks with Python and Hugging Face

    14/11/2024 | Python

  • Building Powerful Command-Line Interfaces with Click and Typer in Python

    15/01/2025 | Python

  • Mastering Imbalanced Data Handling in Python with Scikit-learn

    15/11/2024 | Python

Popular Category

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