pandas - 将样式应用于索引标签和列标签熊猫数据框
问题描述
我有一个数据框,当转储到 excel 时显示如下:
我需要使用格式转储到excel,使其显示为:
即我有一个字典,用于将颜色应用于列名和索引名。
colorIndex = {'A':'Bb', 'B':'B'}
colorColumn = {'ATC':'X1', 'P25':'Y'}
我正在使用以下代码生成数据框并转储到 excel:
import pandas as pd, numpy as np, sys, os
def getDF():
df = pd.DataFrame()
df['ATC'] =np.random.rand(1, 7).round(2).flatten()
df['P25'] =np.random.rand(1, 7).round(2).flatten()
df['P75'] =np.random.rand(1, 7).round(2).flatten()
df['Type1'] = ['A', 'B', 'B', 'A', 'B', 'B', 'A']
df['Type11'] = ['A', 'Aa', 'Bb', 'A', 'Bb', 'B', 'Bb']
df['Type2'] = ['X', 'X', 'X1', 'Y', 'Y', 'Y1', 'Y']
df = df.pivot_table(index=['Type1', 'Type11'], columns='Type2', aggfunc=[np.mean])['mean']
return df
df = getDF()
fn = r'C:\Users\Desktop\format_file.xlsx'
df.to_excel(fn, engine='openpyxl')
但我不知道如何为这种 excel 转储生成样式参数。
解决方案
推荐阅读
- java - 通过使用 Java 的 ExecutorService 的竞争条件?
- php - 如何解决方法不允许必须是以下之一:PUT IN php
- css - 将图标添加到 CSS 边框的末尾
- machine-learning - 训练模型后的最佳权重是多少?最低损失?验证损失?
- javascript - 地图如何使用js处理(字符串,数组列表)?
- matlab - MATLAB 2014b getframe 导致 UI 为“空白”
- python-3.x - IndexError: List Index out of range Keras Tokenizer
- java - Java 对象列表按未排序的特定对象排序
- javascript - 如何使用 php 在不损失质量的情况下增加图像大小
- pdf - 使用 imagemagick 将 pdf 转换为图像并且缺少字体时指定默认替换字体