import pandas as pd
from itables import init_notebook_mode
from itables import show
import itables.options as opt
#raise the size of the data made availible othewize medium (or even some small) dataframes will be downsampled.
opt.maxBytes = 131072
opt.maxColumns = 0
init_notebook_mode(all_interactive=True)
opt.ordering = False Purpose of the blog
As much of the data I use is tabular - I often have to visualize the dataframe, pivot it, filter it, and then visualize it again. Since I may want to go back to the dataset later - making sure I can repeat this mini-EDA again - is quite useful. Downloading an excel for manual exploration is obviously an option but not an ideal one.
Python has an itables library that helps render dataframes as pretty tables. These can also be integrated with quarto to render into the html.
This blog summarizes several key features and how to turn them on.
Example data
Lets generate a complex set of data that has a diverse set of columns
data_for_df = {
'product_id': [101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115],
'price': [12.50, 19.99, 5.00, 25.75, 45.20, 10.99, 8.50, 30.00, 9.99, 15.25, 20.00, 7.50, 22.40, 18.99, 35.10],
'category': ['Electronics', 'Books', 'Groceries', 'Electronics', 'Books', 'Groceries', 'Electronics', 'Books', 'Groceries', 'Electronics', 'Books', 'Groceries', 'Electronics', 'Books', 'Groceries'],
'customer_rating': [4, 5, 3, 5, 4, 5, 4, 3, 5, 4, 5, 3, 4, 5, 4],
'manufacturer': [
'<p><b>Advanced Technology Solutions, a global leader</b><p>',
'Independent Authors Collective, specializing in unique fiction',
'Small-Town Organic Farmers Cooperative',
'Global Tech Innovations Group',
'The Classic Book Reprint Company, preserving literary heritage',
'Fresh Harvest Supply Chain and Distribution',
'Audio Excellence, an innovative headphones manufacturer',
'The Aspiring Coder\'s Press and Digital Media',
'Sustainable Coffee Roasters Association',
'Smart Living Devices and Automation Solutions',
'Renowned Historical Accounts Publishing House',
'Local Produce Delivery for the Modern Consumer',
'Elite Computing Innovations, a leader in high-performance hardware',
'Emerging Voices Poetry Guild',
'Professional Imaging and Optics Corporation'
],
'region': ['North America', 'Europe', 'Asia', 'North America', 'Europe', 'Asia', 'North America', 'Europe', 'Asia', 'North America', 'Europe', 'Asia', 'North America', 'Europe', 'Asia'],
'long_description': [
'This is a long description for the first product, highlighting its premium features and durability.',
'An enthralling novel with a captivating plot and unforgettable characters that will keep you on the edge of your seat.',
'Freshly sourced organic apples, perfect for a healthy snack or for baking delicious homemade pies.',
'A high-performance gadget with advanced technology and a sleek, modern design that is easy to use and carry.',
'A classic mystery story with an unexpected twist, a great read for all fans of the genre.',
'A selection of artisanal breads, baked fresh daily with high-quality ingredients and a crispy crust.',
'Innovative noise-canceling headphones providing crystal-clear audio and a comfortable fit for extended listening sessions.',
'A comprehensive guide to coding best practices, offering practical examples and detailed explanations for beginners.',
'Gourmet coffee beans sourced from sustainable farms, roasted to perfection for a rich and smooth flavor.',
'A versatile smart home device that can control various appliances and is compatible with most other systems.',
'A riveting biography of a historical figure, full of intriguing details and little-known anecdotes from their life.',
'Hand-picked seasonal vegetables delivered fresh to your door, perfect for preparing a wide variety of meals.',
'An ultra-thin tablet with a high-resolution display, making it ideal for both entertainment and professional use.',
'A timeless poetry collection featuring works from both well-known and up-and-coming authors.',
'A powerful and compact camera perfect for capturing high-quality photos and videos on the go.'
]
}
df = pd.DataFrame.from_dict(data_for_df)Now lets see some ways this can be worked with
Default visual
show(df)
Loading ITables v2.5.2 from the init_notebook_mode cell...
(need help?) |
Advanced version
Lets make some changes so that we can modify column widths, add word-wrap, and change defaults for specific columns
show(
df,
# Disable auto-width and use a fixed layout for reliable column width.
autoWidth=False,
style="table-layout:fixed",#; width:800px;",
# Add buttons to customie actions that can be taken
buttons=['pageLength', 'csvHtml15','colvis'],
# Add a search pagen to ensure that I can filter the data in some way
layout={"top1":'searchPanes'},
# Modify columns
columnDefs=[
# Modify width of overly wide columns and make them word wrap
{"width": "200px", "targets": [4, 6], "className": "word-wrap"},
# Hide some columns by default
{"targets": [2,3], "visible": False},
],
scrollX=False,
)
Loading ITables v2.5.2 from the init_notebook_mode cell...
(need help?) |