Sorry for the horrible code. I’m somewhat familiar with Pandas library but have never styled before, so please excuse any horrible patterns. I’m trying to learn styling, so general tips welcome but I’ll keep my questions specific.
The code below produces a styled DF like this:-
Questions:
-
How could I style the text on a specific row of the multi-index in the columns? I want to make the strings ‘North America’, ‘Europe’ and ‘Asia’ larger, for example. I’m not sure how to access them since the styling seems to be based on HTML and the TH is affecting more than I want it to.
-
How could I add a tooltip in a couple of specific cells? For example, in
(Europe, Monthly, Feb)[KPI JKL]
and(Asia, Weekly, WK-04)[KPI GHI]
. The documentation for Styler.set_tooltips is suggesting to create a new DF and apply it. This seems difficult to manage: what if I had a report with dozens of KPIs and more weeks, but only a couple of call outs to highlight via tool tips? -
Is it possible to add tooltips to the KPI names in the index? For example, if the user hovers over
KPI ABC
, it would be good to surface the definition for the KPI. From what I can read in the documentation, tooltips cannot be applied to index or headers..?
# Create an empty DF, use MultiIndex to establish columns
df = pd.DataFrame(index=pd.Index([
'KPI ABC',
'KPI DEF',
'KPI GHI',
'KPI JKL']),
columns=pd.MultiIndex.from_product([
['North America', 'Europe', 'Asia'],
['Weekly', 'Monthly'],
['WK-01', 'WK-02', 'WK-03', 'WK-04', 'WK-05', 'WoW', 'Jan', 'Feb', 'Mar', 'MoM']]))
# Hacky approach -- using the from_product function of Pandas MultiIndex creates unncessary products, so using the hide function to remove them
s = df.style.format('{:.0f}').hide([
('North America', 'Weekly', 'Jan'),
('North America', 'Weekly', 'Feb'),
('North America', 'Weekly', 'Mar'),
('North America', 'Weekly', 'MoM'),
('North America', 'Monthly', 'WK-01'),
('North America', 'Monthly', 'WK-02'),
('North America', 'Monthly', 'WK-03'),
('North America', 'Monthly', 'WK-04'),
('North America', 'Monthly', 'WK-05'),
('North America', 'Monthly', 'WoW'),
('Europe', 'Weekly', 'Jan'),
('Europe', 'Weekly', 'Feb'),
('Europe', 'Weekly', 'Mar'),
('Europe', 'Weekly', 'MoM'),
('Europe', 'Monthly', 'WK-01'),
('Europe', 'Monthly', 'WK-02'),
('Europe', 'Monthly', 'WK-03'),
('Europe', 'Monthly', 'WK-04'),
('Europe', 'Monthly', 'WK-05'),
('Europe', 'Monthly', 'WoW'),
('Asia', 'Weekly', 'Jan'),
('Asia', 'Weekly', 'Feb'),
('Asia', 'Weekly', 'Mar'),
('Asia', 'Weekly', 'MoM'),
('Asia', 'Monthly', 'WK-01'),
('Asia', 'Monthly', 'WK-02'),
('Asia', 'Monthly', 'WK-03'),
('Asia', 'Monthly', 'WK-04'),
('Asia', 'Monthly', 'WK-05'),
('Asia', 'Monthly', 'WoW')],
axis="columns")
# Highlights the full row the user is hovering over, highlights the specific cell with the given background color
cell_hover = {
'selector': 'td:hover',
'props': [('background-color', '#ffffb3')]}
headers = {
'selector': 'th:not(.index_name)',
'props': 'background-color: #000066; color: white;'}
global_left_align = {
'selector': 'th',
'props': 'text-align: left'}
s.set_table_styles([cell_hover, headers, global_left_align])
# Creates the line borders
s.set_table_styles({
('North America', 'Weekly', 'WoW'): [
{'selector': 'th', 'props': 'border-right: 1px solid white'},
{'selector': 'td', 'props': 'border-right: 1px solid #000066'}]
}, overwrite=False, axis=0)
s.set_table_styles({
('North America', 'Weekly', 'WoW'): [
{'selector': 'th', 'props': 'border-left: 1px solid white'},
{'selector': 'td', 'props': 'border-left: 1px solid #000066'}]
}, overwrite=False, axis=0)
s.set_table_styles({
('North America', 'Monthly', 'MoM'): [
{'selector': 'th', 'props': 'border-right: 1px solid white'},
{'selector': 'td', 'props': 'border-right: 1px solid #000066'}]
}, overwrite=False, axis=0)
s.set_table_styles({
('North America', 'Monthly', 'MoM'): [
{'selector': 'th', 'props': 'border-left: 1px solid white'},
{'selector': 'td', 'props': 'border-left: 1px solid #000066'}]
}, overwrite=False, axis=0)
s.set_table_styles({
('Europe', 'Weekly', 'WoW'): [
{'selector': 'th', 'props': 'border-right: 1px solid white'},
{'selector': 'td', 'props': 'border-right: 1px solid #000066'}]
}, overwrite=False, axis=0)
s.set_table_styles({
('Europe', 'Weekly', 'WoW'): [
{'selector': 'th', 'props': 'border-left: 1px solid white'},
{'selector': 'td', 'props': 'border-left: 1px solid #000066'}]
}, overwrite=False, axis=0)
s.set_table_styles({
('Europe', 'Monthly', 'MoM'): [
{'selector': 'th', 'props': 'border-right: 1px solid white'},
{'selector': 'td', 'props': 'border-right: 1px solid #000066'}]
}, overwrite=False, axis=0)
s.set_table_styles({
('Europe', 'Monthly', 'MoM'): [
{'selector': 'th', 'props': 'border-left: 1px solid white'},
{'selector': 'td', 'props': 'border-left: 1px solid #000066'}]
}, overwrite=False, axis=0)
s.set_table_styles({
('Asia', 'Weekly', 'WoW'): [
{'selector': 'th', 'props': 'border-right: 1px solid white'},
{'selector': 'td', 'props': 'border-right: 1px solid #000066'}]
}, overwrite=False, axis=0)
s.set_table_styles({
('Asia', 'Weekly', 'WoW'): [
{'selector': 'th', 'props': 'border-left: 1px solid white'},
{'selector': 'td', 'props': 'border-left: 1px solid #000066'}]
}, overwrite=False, axis=0)
s.set_table_styles({
('Asia', 'Monthly', 'MoM'): [
{'selector': 'th', 'props': 'border-right: 1px solid white'},
{'selector': 'td', 'props': 'border-right: 1px solid #000066'}]
}, overwrite=False, axis=0)
s.set_table_styles({
('Asia', 'Monthly', 'MoM'): [
{'selector': 'th', 'props': 'border-left: 1px solid white'},
{'selector': 'td', 'props': 'border-left: 1px solid #000066'}]
}, overwrite=False, axis=0)
# Center the column headings
s.set_table_styles([
{'selector': 'th.col_heading', 'props': 'text-align: center;'}],overwrite=False)