Elegant Display of Multiple DataFrame Tables in Jupyter Notebook

Nov 17, 2025 · Programming · 17 views · 7.8

Keywords: Jupyter Notebook | pandas DataFrame | IPython.display

Abstract: This article provides a comprehensive guide on displaying multiple pandas DataFrame tables simultaneously in Jupyter Notebook environments. By leveraging the IPython.display module's display() and HTML() functions, it addresses common issues with default output formats. The content includes detailed code examples, pandas display configuration options, and best practices for achieving clean, readable data presentations.

Problem Background and Challenges

When working with Jupyter Notebook for data analysis, a common issue arises with pandas DataFrame display behavior: when outputting multiple DataFrames in the same code cell, only the last DataFrame appears in the beautiful table format, while previous ones may display as plain text or in other undesirable formats. This limitation hampers efficient data exploration and result presentation.

Core Solution: IPython.display Module

The key to resolving simultaneous display of multiple DataFrames lies in utilizing IPython's display module, which provides specialized functions for rendering rich content in Notebooks.

Basic Usage of display() Function

The most straightforward solution involves using the display() function:

from IPython.display import display

# Assuming df1 and df2 are predefined DataFrames
print("DataFrame 1:")
display(df1)
print("DataFrame 2:")
display(df2)

This approach ensures each DataFrame displays independently in full table format, avoiding format conflicts inherent in default behavior.

Advanced Control with HTML Rendering

For scenarios requiring finer control over display formatting, combine with the HTML() function:

from IPython.display import display, HTML

print("DataFrame 1:")
display(HTML(df1.to_html()))
print("DataFrame 2:")
display(HTML(df2.to_html()))

Note that directly using print(df.to_html()) only outputs raw HTML code without browser rendering into tables.

Pandas Display Configuration Optimization

Beyond the display module, proper configuration of pandas display options significantly enhances table readability, especially with wide tables or large datasets.

Temporary Display Configuration

For one-time display needs, use context managers to temporarily modify display options:

import pandas as pd

with pd.option_context('display.max_rows', None, 'display.max_columns', None, 'display.max_colwidth', -1):
    display(df)

This configuration disables limits on rows and columns while ensuring complete column content display, ideal for viewing full datasets.

Global Display Settings

To maintain consistent display effects across multiple cells, set global options:

pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 50)
pd.set_option('display.width', 1000)

These settings affect all subsequent DataFrame displays but require attention to compatibility between different options.

Practical Application Example

Here's a complete example demonstrating how to integrate these techniques in actual analytical work:

import pandas as pd
from IPython.display import display

# Create sample data
df1 = pd.DataFrame({'A': range(10), 'B': range(10, 20)})
df2 = pd.DataFrame({'X': ['apple', 'banana'], 'Y': [100, 200]})

# Optimize display configuration
with pd.option_context('display.max_rows', None, 'display.max_columns', None):
    print("Dataset 1 - Numerical Data:")
    display(df1)
    print("\nDataset 2 - Categorical Data:")
    display(df2)

Best Practice Recommendations

Based on practical experience, we recommend the following best practices:

Conclusion

By effectively leveraging the IPython.display module and pandas display configuration options, users gain full control over DataFrame presentation in Jupyter Notebook. These techniques not only solve the challenge of simultaneous multiple DataFrame display but also significantly improve the efficiency of data exploration and analysis. Mastering these methods is essential for any professional using Python for data analysis.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.