如何在python数据框中为选定列着色?

如何在python数据框中为选定列着色?,python,excel,pandas,dataframe,Python,Excel,Pandas,Dataframe,我正在使用下面的代码将我的df导出到excel,我需要为输出excel中的特定列着色 # DF TO EXCEL from pandas import ExcelWriter writer = ExcelWriter('Output.xlsx') df.to_excel(writer,'sheet1') writer.save() 请向我推荐一种方法。您可以使用: import string np.random.seed(100) df = pd.DataFrame(np.random.ran

我正在使用下面的代码将我的df导出到excel,我需要为输出excel中的特定列着色

# DF TO EXCEL
from pandas import ExcelWriter
writer = ExcelWriter('Output.xlsx')
df.to_excel(writer,'sheet1')
writer.save()
请向我推荐一种方法。

您可以使用:

import string
np.random.seed(100)
df = pd.DataFrame(np.random.randint(10, size=(5,5)), columns=list('GHIJK'))
print (df)
   G  H  I  J  K
0  8  8  3  7  7
1  0  4  2  5  2
2  2  2  1  0  8
3  4  0  9  6  2
4  4  1  5  3  4
如果需要将所有选定列设置为相同颜色:

writer = pd.ExcelWriter('file1.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')

#set format
workbook = writer.book
format = workbook.add_format({'bg_color': 'red'})
worksheet = writer.sheets['Sheet1']

#dict for map excel header, first A is index, so omit it
d = dict(zip(range(25), list(string.ascii_uppercase)[1:]))
print (d)
{0: 'B', 1: 'C', 2: 'D', 3: 'E', 4: 'F', 5: 'G', 6: 'H', 7: 'I', 8: 'J',
 9: 'K', 10: 'L', 11: 'M', 12: 'N', 13: 'O', 14: 'P', 15: 'Q', 16: 'R', 
 17: 'S', 18: 'T', 19: 'U', 20: 'V', 21: 'W', 22: 'X', 23: 'Y', 24: 'Z'}

 #select columns of df
cols = ['G','J','K']

#in loop set background color where are data
#http://xlsxwriter.readthedocs.io/working_with_conditional_formats.html
for col in cols:
    excel_header = str(d[df.columns.get_loc(col)])
    len_df = str(len(df.index) + 1)

    rng = excel_header + '2:' + excel_header + len_df
    worksheet.conditional_format(rng, {'type': 'no_blanks',
                                       'format': format})
writer.save()

如果需要将所有选定列设置为不同的颜色:

writer = pd.ExcelWriter('file1.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')

#set format
workbook = writer.book
worksheet = writer.sheets['Sheet1']

#dict for map excel header, first A is index, so omit it
d = dict(zip(range(25), list(string.ascii_uppercase)[1:]))
print (d)
{0: 'B', 1: 'C', 2: 'D', 3: 'E', 4: 'F', 5: 'G', 6: 'H', 7: 'I', 8: 'J',
 9: 'K', 10: 'L', 11: 'M', 12: 'N', 13: 'O', 14: 'P', 15: 'Q', 16: 'R', 
 17: 'S', 18: 'T', 19: 'U', 20: 'V', 21: 'W', 22: 'X', 23: 'Y', 24: 'Z'}

 #select columns of df
cols = ['G','J','K']
colors = ['red','yellow','blue']

#in loop set background color where are data
#http://xlsxwriter.readthedocs.io/working_with_conditional_formats.html
for i, col in enumerate(cols):
    format = workbook.add_format({'bg_color': colors[i]})
    excel_header = str(d[df.columns.get_loc(col)])
    len_df = str(len(df.index) + 1)

    rng = excel_header + '2:' + excel_header + len_df
    worksheet.conditional_format(rng, {'type': 'no_blanks',
                                       'format': format})
writer.save()

为那些会发现这个问题的人提供一点更新;今天,Pandas包含了样式函数和样式器对象,这非常实用,可以在不使用非常复杂的方法的情况下实现所需的功能

这可能吗???颜色如何?喜欢文本颜色吗?@DYZ有可能,但不确定如何在DF上实现-请检查@Nick T-按列背景颜色我已经用我的数据帧修改了你的脚本,得到一个错误“KeyError:'I'”。有什么线索吗?对不起,这是我的错误,我给了一个无效的列名。它工作正常。非常感谢!:)@jezrael是否有可能根据单个单元格的值对其进行着色?