How to pretty-print an entire Pandas Series or DataFrame in the terminal with full display, proper alignment, borders, and color-coding options?
To pretty-print an entire Pandas Series or DataFrame in the terminal with full display, proper alignment, borders, and color-coding, you can use a combination of Pandas’ built-in display options, styling methods, and terminal-specific formatting techniques. The key approaches include setting display options for full visibility, using the style attribute for advanced formatting, and leveraging terminal color capabilities for enhanced readability.
Contents
- Basic Display Configuration
- Advanced Display Options
- Styling and Formatting
- Color Coding and Visual Enhancement
- Terminal-Specific Considerations
- Practical Examples
Basic Display Configuration
Pandas provides several configuration options to control how objects are displayed in the terminal. These settings ensure your entire dataset is visible with proper formatting.
Displaying Full DataFrames
The pd.set_option() function allows you to control display behavior:
import pandas as pd
# Display all rows and columns
pd.set_option('display.max_rows', None) # Show all rows
pd.set_option('display.max_columns', None) # Show all columns
pd.set_option('display.width', None) # No width restriction
pd.set_option('display.max_colwidth', None) # Show full column content
Display Precision Control
Control the number of decimal places displayed:
pd.set_option('display.precision', 2) # Show 2 decimal places
pd.set_option('display.float_format', '{:.2f}'.format) # Format all floats
Context Manager for Temporary Settings
For temporary display changes without affecting global settings:
with pd.option_context('display.max_rows', None, 'display.max_columns', None):
print(your_dataframe)
Advanced Display Options
Using to_string() Method
The to_string() method provides extensive customization:
df.to_string(
max_rows=None, # Show all rows
max_cols=None, # Show all columns
line_width=None, # No width limit
col_space=None, # Auto column spacing
header=True, # Show column headers
index=True, # Show row indices
na_rep='NA', # Missing value representation
justify='left', # Column alignment
float_format='%.2f', # Float formatting
sparsify=True, # Remove duplicate index names
show_dimensions=True # Show dataframe dimensions
)
Formatting Parameters
justify: ‘left’, ‘right’, ‘center’, or ‘justify’col_space: Minimum width for each columndecimal: Decimal separatorthousands: Thousands separator
HTML Display Integration
For enhanced terminal capabilities:
from IPython.display import HTML
HTML(df.to_html())
Styling and Formatting
DataFrame Styling with style
Pandas styling provides powerful formatting capabilities:
# Basic styling
df.style.format({
'numeric_column': '{:.2f}',
'percentage_column': '{:.1%}'
})
# Conditional formatting
def highlight_max(s):
is_max = s == s.max()
return ['background-color: yellow' if v else '' for v in is_max]
df.style.apply(highlight_max)
Styling Methods
style.format(): Format specific columnsstyle.apply(): Apply custom styling functionsstyle.background_gradient(): Color gradientsstyle.bar(): Bar charts within cellsstyle.set_properties(): Set CSS properties
Combining Multiple Styles
(df.style
.format({'price': '${:,.2f}'})
.background_gradient(subset=['sales'], cmap='Greens')
.apply(highlight_max, subset=['profit'])
.set_table_styles([{
'selector': 'th',
'props': [('background-color', '#f7f7f9')]
}]))
Color Coding and Visual Enhancement
Built-in Color Maps
Pandas supports various color schemes:
# Background gradients
df.style.background_gradient(cmap='viridis')
# Heatmap style
df.style.background_gradient(cmap='RdYlBu', subset=['numeric_columns'])
# Bar charts
df.style.bar(color='lightblue', subset=['numeric_columns'])
Custom Color Functions
Create custom color coding:
def color_negative_red(val):
color = 'red' if val < 0 else 'green'
return f'color: {color}'
df.style.applymap(color_negative_red)
Color Schemes by Data Type
# Numeric columns - blue gradient
numeric_cols = df.select_dtypes(include=['number']).columns
# Categorical columns - yellow background
categorical_cols = df.select_dtypes(include=['object']).columns
(df.style
.background_gradient(cmap='Blues', subset=numeric_cols)
.applymap(lambda x: 'background-color: yellow'
if isinstance(x, str) else '', subset=categorical_cols))
Text Styling
def bold_text(val):
return 'font-weight: bold'
df.style.applymap(bold_text, subset=['important_columns'])
Terminal-Specific Considerations
Terminal Color Support
Check and leverage terminal color capabilities:
import sys
print(sys.stdout.isatty()) # Check if terminal supports color
ANSI Color Codes
For direct terminal color formatting:
class Colors:
RED = '\033[91m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
BLUE = '\033[94m'
MAGENTA = '\033[95m'
CYAN = '\033[96m'
WHITE = '\033[97m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
print(f"{Colors.BOLD}{Colors.GREEN}Success!{Colors.END}")
Rich Library Integration
The rich library provides advanced terminal formatting:
from rich.console import Console
from rich.table import Table
console = Console()
table = Table(show_header=True, header_style="bold magenta")
table.add_column("Column 1", style="dim")
table.add_column("Column 2", justify="right")
for row in df.itertuples():
table.add_row(str(row[1]), str(row[2]))
console.print(table)
Terminal Size Detection
import shutil
terminal_width = shutil.get_terminal_size().columns
terminal_height = shutil.get_terminal_size().lines
print(f"Terminal size: {terminal_width}x{terminal_height}")
Practical Examples
Complete Pretty-Print Function
def pretty_print_dataframe(df, max_rows=None, max_cols=None,
highlight_numeric=True, color_scheme='viridis'):
"""
Enhanced pretty-print function for DataFrames with full display options.
"""
# Set display options
with pd.option_context('display.max_rows', max_rows,
'display.max_columns', max_cols,
'display.width', None,
'display.precision', 2):
# Apply styling if requested
if highlight_numeric:
numeric_cols = df.select_dtypes(include=['number']).columns
styled = df.style.background_gradient(cmap=color_scheme, subset=numeric_cols)
return styled
else:
return df
# Usage
pretty_print_dataframe(your_dataframe, max_rows=50, color_scheme='plasma')
Financial Data Example
import numpy as np
# Create sample financial data
np.random.seed(42)
dates = pd.date_range('2024-01-01', periods=10)
financial_data = pd.DataFrame({
'Date': dates,
'Revenue': np.random.uniform(1000, 5000, 10),
'Expenses': np.random.uniform(500, 2000, 10),
'Profit': np.random.uniform(200, 3000, 10),
'Growth_Rate': np.random.uniform(-0.1, 0.3, 10)
})
# Format and display
financial_formatted = (financial_data.style
.format({
'Revenue': '${:,.2f}',
'Expenses': '${:,.2f}',
'Profit': '${:,.2f}',
'Growth_Rate': '{:.1%}'
})
.background_gradient(cmap='RdYlGn', subset=['Profit'])
.applymap(lambda x: 'color: red' if x < 0 else 'color: green',
subset=['Growth_Rate'])
.set_table_styles([{
'selector': 'th',
'props': [('background-color', '#f8f9fa'),
('color', '#495057'),
('font-weight', 'bold')]
}])
.set_properties(**{'text-align': 'right'}, subset=['Revenue', 'Expenses', 'Profit'])
.set_properties(**{'text-align': 'center'}))
financial_formatted
Statistical Summary with Color Coding
# Create statistical summary
stats_summary = df.describe().T
# Enhanced statistical summary display
(stats_summary.style
.format({'count': '{:.0f}',
'mean': '{:.2f}',
'std': '{:.2f}',
'min': '{:.2f}',
'25%': '{:.2f}',
'50%': '{:.2f}',
'75%': '{:.2f}',
'max': '{:.2f}'})
.background_gradient(cmap='coolwarm', subset=['mean', 'std'])
.applymap(lambda x: 'font-weight: bold' if x > stats_summary['mean'].mean() else '',
subset=['mean'])
.set_caption('Statistical Summary with Color Coding')
.set_table_styles([
{'selector': 'caption',
'props': [('caption-side', 'top'),
('font-size', '16px'),
('font-weight', 'bold'),
('color', '#2c3e50')]}
]))
Conclusion
To effectively pretty-print Pandas DataFrames and Series in your terminal, combine Pandas’ built-in display options with advanced styling techniques. Start with basic display configuration using pd.set_option() to ensure full visibility, then leverage the style attribute for sophisticated formatting including color gradients, conditional formatting, and custom CSS styling. For enhanced terminal capabilities, consider integrating libraries like rich or using ANSI color codes directly. Remember to balance visual appeal with readability, especially for large datasets, and use context managers for temporary display changes without affecting your global settings. Experiment with different color schemes and formatting options to find the presentation that best suits your data and analytical needs.