将样式化的数据框导出到Excel(背景颜色)

huangapple go评论89阅读模式
英文:

Export styled dataframe to Excel (background color)

问题

I can help you with the Chinese translation. Here is the translated text:

  1. 我想要从样式化的数据框生成一个Excel文件。具体来说,我想保留单元格的背景颜色。
  2. [![我的样式化数据框][1]][1]
  3. 我可以使用`df.to_excel()``df.style.to_excel()`生成Excel文件,但背景颜色在生成过程中消失了。有没有办法保留背景颜色?
  4. [![在此输入图片描述][2]][2]
  5. 编辑:我使用的具体代码如下:
  6. ```python
  7. df = pd.DataFrame([
  8. { 'color_A_in_red': True , 'A': 1 },
  9. { 'color_A_in_red': False , 'A': 2 },
  10. { 'color_A_in_red': True , 'A': 2 },
  11. ])
  12. def highlight(x):
  13. c = "background-color:red"
  14. #条件
  15. m = x["color_A_in_red"]
  16. #样式的数据框
  17. df1 = pd.DataFrame('', index=x.index, columns=x.columns)
  18. #按条件设置列
  19. df1.loc[m, 'A'] = c
  20. return df1
  21. df.style.apply(highlight, axis=None)
  22. df.to_excel("test.xlsx")
  1. Please note that I have retained the code part without translation as requested.
  2. <details>
  3. <summary>英文:</summary>
  4. I&#39;m looking to generate an excel file from styled dataframes. Specifically I want to keep the background colors of cells.
  5. [![My styled dataframe][1]][1]
  6. I can generate an excel file with df.to_excel() or df.style.to_excel(), but the background color disappears along the way. Is there any way to keep the background color?
  7. [![enter image description here][2]][2]
  8. [1]: https://i.stack.imgur.com/TOJe9.png
  9. [2]: https://i.stack.imgur.com/smlYQ.png
  10. EDIT: The specific code I used:

df = pd.DataFrame([
{ 'color_A_in_red': True , 'A': 1 },
{ 'color_A_in_red': False , 'A': 2 },
{ 'color_A_in_red': True , 'A': 2 },
])

def highlight(x):
c = f"background-color:red"
#condition
m = x["color_A_in_red"]
# DataFrame of styles
df1 = pd.DataFrame('', index=x.index, columns=x.columns)
# set columns by condition
df1.loc[m, 'A'] = c
return df1

df.style.apply(highlight, axis=None)
df.to_excel("test.xlsx")

  1. </details>
  2. # 答案1
  3. **得分**: 0
  4. df.style.apply() 不会直接改变df,而是返回一个修改后的副本。以下代码有效:
  5. ```python
  6. df = pd.DataFrame([
  7. { 'color_A_in_red': True , 'A': 1 },
  8. { 'color_A_in_red': False , 'A': 2 },
  9. { 'color_A_in_red': True , 'A': 2 },
  10. ])
  11. def highlight(x):
  12. c = "background-color:red"
  13. # 条件
  14. m = x["color_A_in_red"]
  15. # 样式的DataFrame
  16. df1 = pd.DataFrame('', index=x.index, columns=x.columns)
  17. # 根据条件设置列
  18. df1.loc[m, 'A'] = c
  19. return df1
  20. df = df.style.apply(highlight, axis=None)
  21. df.to_excel("test.xlsx")

将样式化的数据框导出到Excel(背景颜色)

英文:

df.style.apply() does not change df directly but returns a modified copy. The following code works:

  1. df = pd.DataFrame([
  2. { &#39;color_A_in_red&#39;: True , &#39;A&#39;: 1 },
  3. { &#39;color_A_in_red&#39;: False , &#39;A&#39;: 2 },
  4. { &#39;color_A_in_red&#39;: True , &#39;A&#39;: 2 },
  5. ])
  6. def highlight(x):
  7. c = f&quot;background-color:red&quot;
  8. #condition
  9. m = x[&quot;color_A_in_red&quot;]
  10. # DataFrame of styles
  11. df1 = pd.DataFrame(&#39;&#39;, index=x.index, columns=x.columns)
  12. # set columns by condition
  13. df1.loc[m, &#39;A&#39;] = c
  14. return df1
  15. df = df.style.apply(highlight, axis=None)
  16. df.to_excel(&quot;test.xlsx&quot;)

将样式化的数据框导出到Excel(背景颜色)

huangapple
  • 本文由 发表于 2023年3月31日 22:43:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899838.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定