Pandas: Specific Styling with Multi-Index Columns (Tooltips)

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:

  1. 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.

  2. 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?

  3. 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)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật