Pandas 多索引数据透视表将字体权重从';粗体';至';正常';

Pandas 多索引数据透视表将字体权重从';粗体';至';正常';,pandas,pivot-table,Pandas,Pivot Table,如何获得“小计”、“大总计”和那些值“粗体”以及“变化”下字体的其余部分,并将其计算为“正常”字体重量,就像我的图像一样?或者任何其他方式,我可以让小计和总计弹出数据框的其余部分。难题。。。您需要找到一种方法来选择某些标记 让我们尝试一下Jupyter笔记本电脑的CSS选择器编码 def styleme(x): fw = 'bold' if 'Total' in x.name[1] else 'normal' l = [f'font-weight: {fw}']*len(x)


如何获得“小计”、“大总计”和那些值“粗体”以及“变化”下字体的其余部分,并将其计算为“正常”字体重量,就像我的图像一样?或者任何其他方式,我可以让小计和总计弹出数据框的其余部分。

难题。。。您需要找到一种方法来选择某些标记

让我们尝试一下Jupyter笔记本电脑的CSS选择器编码

def styleme(x):
    fw = 'bold' if 'Total' in x.name[1] else 'normal'
    l = [f'font-weight: {fw}']*len(x)
    return l

total_idx = np.where(~df.index.get_level_values(1).str.contains('Total'))

table_style = [{'selector':f'tbody tr th[id*=level1_row{i}]', 'props':[('font-weight','normal')]} for i in total_idx[0]]
df.style.apply(styleme, axis=1).set_table_styles(table_style)
输出:

但是,与之前一样,在上一篇文章中,这种格式设置并没有在to_excel输出中延续到excel:


难题。。。您将需要查找以选择某些标记。我在这篇文章中也做了类似的事情
def styleme(x):
    fw = 'bold' if 'Total' in x.name[1] else 'normal'
    l = [f'font-weight: {fw}']*len(x)
    return l

total_idx = np.where(~df.index.get_level_values(1).str.contains('Total'))

table_style = [{'selector':f'tbody tr th[id*=level1_row{i}]', 'props':[('font-weight','normal')]} for i in total_idx[0]]
df.style.apply(styleme, axis=1).set_table_styles(table_style)