Python 为 Pandas 中的细胞着色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28075699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:42:29  来源:igfitidea点击:

Coloring Cells in Pandas

pythonpandasipython

提问by Rahul Wadhwani

I am able to import data from an excel file using Pandas by using:

我可以使用 Pandas 从 Excel 文件中导入数据:

xl = read_excel('path_to_file.xls', 'Sheet1', index_col=None, na_values=['NA'])    

Now that I have all the data in xl as DataFrame. I would like to colour some cells in that data based on conditions defined in another function and export the same (with colour coding) to an Excel file.

现在我将 xl 中的所有数据都作为 DataFrame。我想根据另一个函数中定义的条件为该数据中的某些单元格着色,并将相同的(带有颜色编码)导出到 Excel 文件。

Can someone tell me how should I go about this?

有人能告诉我我应该怎么做吗?

Thank you.

谢谢你。

采纳答案by wkzhu

Pandas has a relatively new Stylerfeature where you can apply conditional formatting type manipulations to dataframes. http://pandas.pydata.org/pandas-docs/stable/style.html

Pandas 有一个相对较新的Styler功能,您可以在其中对数据框应用条件格式类型操作。 http://pandas.pydata.org/pandas-docs/stable/style.html

You can use some of their built-in functions like background_gradientor barto replicate excel-like features like conditional formatting and data bars. You can also format cells to display percentages, floats, ints, etc. without changing the original dataframe.

您可以使用它们的一些内置功能,例如background_gradientbar复制条件格式和数据栏等类似 excel 的功能。您还可以格式化单元格以显示百分比、浮点数、整数等,而无需更改原始数据框。

Here's an example of the type of chart you can make using Styler(this is a nonsense chart but just meant to demonstrate features):

这是您可以使用的图表类型的示例Styler(这是一个无意义的图表,但只是为了演示功能):

enter image description here

在此处输入图片说明

To harness the full functionality of Styleryou should get comfortable with the Styler.apply()and Styler.applymap()APIs. These allow you to create custom functions and apply them to the table's columns, rows or elements. For example, if I wanted to color a +ive cell green and a -ive cell red, I'd create a function

要利用您的全部功能,Styler您应该熟悉Styler.apply()Styler.applymap()API。这些允许您创建自定义函数并将它们应用于表的列、行或元素。例如,如果我想将 +ive 单元格着色为绿色,将 -ive 单元格着色为红色,我将创建一个函数

def _color_red_or_green(val):
    color = 'red' if val < 0 else 'green'
    return 'color: %s' % color

and call it on my Stylerobject, i.e., df.style.applymap(_color_red_or_green).

并在我的Styler对象上调用它,即df.style.applymap(_color_red_or_green)

With respect to exporting back to Excel, as far as I'm aware this is not supported in Styleryet so I'd probably go the xlsxwriter route if you NEED Excel for some reason. However, in my experience this is a great pure Python alternative, for example along with matplotlib charts and in emails/reports.

关于导出回 Excel,据我所知,Styler目前尚不支持此功能,因此如果您出于某种原因需要 Excel,我可能会选择 xlsxwriter 路线。然而,根据我的经验,这是一个很好的纯 Python 替代方案,例如与 matplotlib 图表和电子邮件/报告一起使用。

回答by Skorpeo

try something like this:

尝试这样的事情:

with pandas.io.excel.ExcelWriter(path=Path, engine="xlsxwriter") as writer:
   sheet = writer.book.worksheets()[0]
   sheet.write(x, y, value, format) #format is what determines the color etc.

More info here: https://xlsxwriter.readthedocs.org/format.html

更多信息在这里:https: //xlsxwriter.readthedocs.org/format.html

回答by pyano

There are quite a few ideas about styling the cells on the Pandas website. However it ist mentioned: This is a new feature and still under development. We'll be adding features and possibly making breaking changes in future releases

Pandas 网站上有很多关于单元格样式的想法。但是它被提到:这是一个新功能,仍在开发中。我们将添加功能并可能在未来版本中进行重大更改