根据此示例,该to_excel方法应保存带有背景颜色的 Excel 文件。但是,我保存的 Excel 文件中没有任何颜色。我尝试同时使用openpyxl和xlsxwriter引擎进行编写。在这两种情况下,Excel 文件都保存了,但单元格颜色/样式丢失了。
我可以读回文件并使用 重新格式化openpyxl,但如果这种to_excel方法应该有效,为什么不呢?
这是示例代码。
import pandas as pd # version 0.24.2
dict = {'A': [1, 1, 1, 1, 1], 'B':[2, 1, 2, 1, 2], 'C':[1, 2, 1, 2, 1]}
df = pd.DataFrame(dict)
df_styled = df.style.apply(lambda x: ["background: #ffa31a" if x.iloc[0] < v else " " for v in x], axis=1)
df_styled
''' in my jupyter notebook, this displayed my dataframe with background color …Run Code Online (Sandbox Code Playgroud)