Skip to content

Instantly share code, notes, and snippets.

@izikeros
Last active May 13, 2026 15:45
Show Gist options
  • Select an option

  • Save izikeros/b0d32072f234fba73650eb4b1e9c0017 to your computer and use it in GitHub Desktop.

Select an option

Save izikeros/b0d32072f234fba73650eb4b1e9c0017 to your computer and use it in GitHub Desktop.
[DataFrame in console with rich] Use Rich library to display Pandas DataFrame in console #pandas

rich_display_dataframe

rich_display_dataframe is a Python function that allows you to display a Pandas DataFrame as a table using the rich library.

rich_dataframe

Usage

def rich_display_dataframe(df, title="Dataframe") -> None:
    """Display dataframe as table using rich library.
    
    Args:
        df (pd.DataFrame): dataframe to display
        title (str, optional): title of the table. Defaults to "Dataframe".
    
    Raises:
        NotRenderableError: if dataframe cannot be rendered
        
    Returns:
        rich.table.Table: rich table
    """

Description

The rich_display_dataframe function provides a convenient way to display a Pandas DataFrame as a table with rich formatting using the rich library. It ensures that the DataFrame is rendered properly and only contains string values.

Installation

To use the rich_display_dataframe function, you need to have the rich library installed. You can install it using pip:

pip install rich

Example

Here's an example demonstrating how to use the rich_display_dataframe function:

import pandas as pd

# Create a sample DataFrame
data = {
    "Name": ["John", "Jane", "Alice", "Bob"],
    "Age": [25, 30, 35, 40],
    "City": ["New York", "Paris", "London", "Tokyo"]
}
df = pd.DataFrame(data)

# Display the DataFrame using rich_display_dataframe
rich_display_dataframe(df, title="Sample DataFrame")

This will display the DataFrame as a table using rich formatting:

rich_dataframe

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request.

License

This project is licensed under the MIT License.

import contextlib
from rich.errors import NotRenderableError
def rich_display_dataframe(df, title="Dataframe") -> None:
"""Display dataframe as table using rich library.
Args:
df (pd.DataFrame): dataframe to display
title (str, optional): title of the table. Defaults to "Dataframe".
Raises:
NotRenderableError: if dataframe cannot be rendered
Returns:
rich.table.Table: rich table
"""
from rich import print
from rich.table import Table
# ensure dataframe contains only string values
df = df.astype(str)
table = Table(title=title)
for col in df.columns:
table.add_column(col)
for row in df.values:
with contextlib.suppress(NotRenderableError):
table.add_row(*row)
print(table)
@ColdTeapot273K
Copy link
Copy Markdown

Would be nice to get the scientific notation working, i.e.

import pandas as pd
pd.set_option("display.precision", 2)

Expected:
image

Actual:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment