简体   繁体   English

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

[英]pandas+xlsx: format cells based on another dataframe

I have a pivot table from a dataframe: 我有一个数据框的数据透视表:

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

Here is another pivot table with logical values: 这是另一个具有逻辑值的数据透视表:

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

It is stored to Excel: 它存储到Excel:

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

I need to write it to xlsx file and set cell color according to the second pivot table, ie to set cell color as 75% gray if according value in fl is True. 我需要将其写入xlsx文件并根据第二个数据透视表设置单元格颜色,即,如果fl相应值为True,则将单元格颜色设置为75%的灰色。 How can I do it? 我该怎么做?

If you store both dataframes in the Excel workbook you can use conditional formatting to highlight the cells in one region based on the values in another region. 如果将两个数据框都存储在Excel工作簿中,则可以使用条件格式来基于另一个区域中的值突出显示一个区域中的单元格。 See also Adding Conditional Formatting to Dataframe output . 另请参阅将条件格式添加到数据帧输出

If you only want to add the values dataframe then I would suggest not using pd.ExcelWriter() directly and using XlsxWriter formatting . 如果只想添加值数据pd.ExcelWriter()则建议不要直接使用pd.ExcelWriter()并使用XlsxWriter格式化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas 数据框样式:根据格式列突出显示一些单元格 - Pandas dataframe styling: highlight some cells based on a format column 如何在 Python 中基于另一个 Pandas dataframe 完成 NaN 单元格 - How to complete NaN cells based on another Pandas dataframe in Python 根据另一个数据框中的列有条件地格式化每列中的单元格 - Conditionally format cells in each column based on columns in another dataframe 根据行值将单元格从一个 Pandas 数据帧覆盖到另一个 - overwriting cells from one pandas dataframe to another based on row values Pandas:使用多行格式将包含字符串的数据帧写入 xlsx - Pandas: Write dataframe containing strings to xlsx with multiline format 基于条件切片更改Pandas DataFrame中的单元格 - Change Cells in Pandas DataFrame Based on Conditional Slices 包含 Pandas DataFrame to_string 中浮点数列表的 Pandas 格式单元格 - Pandas format cells containing list of float in Pandas DataFrame to_string Pandas:根据另一列设置数据透视中的单元格样式 - Pandas: Style cells in a pivot based on another column 如何根据 Python pandas DataFrame 中的单元格的值对不同的单元格进行着色 - How to color different cells based on the value of the cells in a Python pandas DataFrame 基于另一个数据框生成熊猫数据框 - Generate pandas dataframe based on another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM