pandas + xlsx:根据另一个数据帧格式化单元格

时间:2017-09-20 04:19:57

标签: python excel pandas xlsxwriter

我有一个来自数据框的数据透视表:

pv=testdata.pivot(index='dose',columns='el_num',values='value').reindex(index=doseann)

el_num          1         2        3        4        5        6        7         8        9        10       11
dose                                                                                                          
100.0      7.07460   6.37422  19.8883  18.6835  16.5359  59.8294  28.5587  14.18910  39.5265  4.33896  38.0297
11931.0    6.41105   8.27059  19.0014  18.6988  16.4000  59.1123  29.4836  13.25030  36.2842  5.89428  37.9752
25079.0    6.82894   8.11478  19.8956  18.8933  15.8732  58.6548  29.8440  13.25930  36.7238  7.37476  39.1368
49640.0    7.20882   8.17981  19.3958  18.0241  15.3036  58.6676  29.9847  12.50980  37.5594  7.81891  38.7749
71545.0    9.57559  11.55590  15.4280  15.8461  13.5970  59.9049  27.4346   8.38379  40.9102  7.78858  38.5024
84303.0    9.69782  11.00110  16.4352  14.9416  13.6581  59.9323  26.3975   9.74285  40.3733  7.85947  38.5113
101415.0  10.60720  10.36910  16.3399  16.9584  13.1570  60.1249  27.9201  11.02400  39.6205  7.64924  39.0897
150913.0  10.70750  10.07470  17.9623  16.1063  13.2890  59.9274  27.7685  11.94690  39.0937  8.43550  39.5281
169885.0  10.39460   0.00000  16.9633  14.7942  13.8830  58.9495  27.9250  12.58740  38.8587  8.10606  38.8391
200463.0   9.59026   9.26161  18.0652  15.2096  13.0975  59.1136  27.8377  11.90810  40.4693  8.51281  39.2943
24.0       9.45291   9.27879  17.9021  16.5391  13.4601  58.9314  27.3388  10.94170  39.0885  8.77127  38.4680
192.0      6.14907   6.94374  19.6765  12.5670  15.6754  56.5163  28.8796  11.78300  36.6076  6.21283  38.8232

这是另一个具有逻辑值的数据透视表:

fl=testdata.pivot(index='dose',columns='el_num',values='fail').reindex(index=doseann)

el_num       1      2      3      4      5     6      7      8     9      10    11
dose                                                                              
100.0     False  False  False  False  False  True  False  False  True  False  True
11931.0   False  False  False  False  False  True  False  False  True  False  True
25079.0   False  False  False  False  False  True  False  False  True  False  True
49640.0   False  False  False  False  False  True  False  False  True  False  True
71545.0   False  False  False  False  False  True  False  False  True  False  True
84303.0   False  False  False  False  False  True  False  False  True  False  True
101415.0  False  False  False  False  False  True  False  False  True  False  True
150913.0  False  False  False  False  False  True  False  False  True  False  True
169885.0  False  False  False  False  False  True  False  False  True  False  True
200463.0  False  False  False  False  False  True  False  False  True  False  True
24.0      False  False  False  False  False  True  False  False  True  False  True
192.0     False  False  False  False  False  True  False  False  True  False  True

存储到Excel:

doc=pd.ExcelWriter('tests.xlsx',engine='xlsxwriter')
pv2=pd.DataFrame(pv)
pv2.to_excel(doc,sheet_name='Sheet1')

我需要将它写入xlsx文件并根据第二个数据透视表设置单元格颜色,即如果fl中的相应值为True,则将单元格颜色设置为75%灰色。我该怎么办?

1 个答案:

答案 0 :(得分:0)

如果将两个数据框存储在Excel工作簿中,则可以使用conditional formatting根据另一个区域中的值突出显示一个区域中的单元格。另请参阅Adding Conditional Formatting to Dataframe output

如果您只想添加值数据框,我建议您不要直接使用pd.ExcelWriter()并使用XlsxWriter formatting