【问题标题】:Why does Excel styling not work in Pandas?为什么 Excel 样式在 Pandas 中不起作用?
【发布时间】:2019-04-13 00:28:10
【问题描述】:
import pandas as pd
import xlsxwriter
from datetime import datetime
import sys

path = sys.argv[1]
xl = pd.ExcelFile(path)
df = xl.parse("Sheet1")
df.columns = ['Nume', 'Tip de', 'Unit', 'Speciale Price', 'Suma de', 'Suma']


def highlight_max(x):
    return ['background-color: yellow' if v == x.max() else ''
        for v in x]

df.style.apply(highlight_max)
df.loc[-1] = ['Totul', '', '', '', df['Suma de'].sum(), df['Suma'].sum()]

我尝试将highlight_max 应用于列

【问题讨论】:

  • 您要读取的path 中文件的格式是什么?

标签: python pandas pandas-styles


【解决方案1】:

因此,我将假设您希望将样式更改应用到 Python 中的数据框,其设计实现方式并在控制台中打印数据框时显示颜色修改。问题是(我假设)您没有使用 IDE Jupyter Notebook(或任何其他基于 Web 的 IDE),它被设计用于使用它。请参阅下面的比较。

来源:Pandas Styling Documentation

样式对象以 html 呈现,其中基于 Web 的 IDE(如 Jupyter Notebook)可以读取并显示修改。

这是对 Spyder IDE 文档的简化。注意输出是一个样式对象。渲染后,您可以在第二个输出中看到 Spyder IDE 无法解释的 html/css。

import pandas as pd

data1 = {'Nume': [1,-2,-3],
         'Tip de':[4,-5,-6],
         'Unit':[-7,-8,9],
         'Speciale Price': [10,11,12],
         'Suma de': [13,14,15],
         'Suma':[16,17,18]}

df1 = pd.DataFrame(data1)
print(df1)

def color_negative_red(val):
    """
    Takes a scalar and returns a string with
    the css property `'color: red'` for negative
    strings, black otherwise.
    """
    color = 'red' if val < 0 else 'black'
    return 'color: %s' % color

s = df1.style.applymap(color_negative_red)
s

Output[] = <pandas.io.formats.style.Styler at 0x18aaa2a6c50>

s.render()

Output[] = '<style  type="text/css" >\n    #T_db7ac00c_6244_11e9_887c_74d4355eed37row0_col0 {\n            color:  black;\n            color:  black;\n        }    #T_db7ac00c_6244_11e9_887c_74d4355eed37row0_col1 {\n            color:  black;\n            color:  black;\n        }    #T_db7ac00c_6244_11e9_887c_74d4355eed37row0_col2 {\n            color:  black;\n            color:  black;\n        }    #T_db7ac00c_6244_11e9_887c_74d4355eed37row0_col3 {\n            color:  black;\n            color:  black;\n        }    #T_db7ac00c_6244_11e9_887c_74d4355eed37row0_col4 {\n            color:  black;\n            color:  black;\n        }    #T_db7ac00c_6244_11e9_887c_74d4355eed37row0_col5 {\n            color:  red;\n            color:  red;\n        }    #T_db7ac00c_6244_11e9_887c_74d4355eed37row1_col0 {\n            color:  red;\n            color:  red;\n        }    #T_db7ac00c_6244_11e9_887c_74d4355eed37row1_col1 {\n            color:  black;\n            color:  black;\n        }    #T_db7ac00c_6244_11e9_887c_74d4355eed37row1_col2 {\n            color:  black;\n            color:  black;\n        }    #T_db7ac00c_6244_11e9_887c_74d4355eed37row1_col3 {\n            color:  black;\n            color:  black;\n        }    #T_db7ac00c_6244_11e9_887c_74d4355eed37row1_col4 {\n            color:  red;\n            color:  red;\n        }    #T_db7ac00c_6244_11e9_887c_74d4355eed37row1_col5 {\n            color:  red;\n            color:  red;\n        }    #T_db7ac00c_6244_11e9_887c_74d4355eed37row2_col0 {\n            color:  red;\n            color:  red;\n        }    #T_db7ac00c_6244_11e9_887c_74d4355eed37row2_col1 {\n            color:  black;\n            color:  black;\n        }    #T_db7ac00c_6244_11e9_887c_74d4355eed37row2_col2 {\n            color:  black;\n            color:  black;\n        }    #T_db7ac00c_6244_11e9_887c_74d4355eed37row2_col3 {\n            color:  black;\n            color:  black;\n        }    #T_db7ac00c_6244_11e9_887c_74d4355eed37row2_col4 {\n            color:  red;\n            color:  red;\n        }    #T_db7ac00c_6244_11e9_887c_74d4355eed37row2_col5 {\n            color:  black;\n            color:  black;\n        }</style>  \n<table id="T_db7ac00c_6244_11e9_887c_74d4355eed37" > \n<thead>    <tr> \n        <th class="blank level0" ></th> \n        <th class="col_heading level0 col0" >Nume</th> \n        <th class="col_heading level0 col1" >Speciale Price</th> \n        <th class="col_heading level0 col2" >Suma</th> \n        <th class="col_heading level0 col3" >Suma de</th> \n        <th class="col_heading level0 col4" >Tip de</th> \n        <th class="col_heading level0 col5" >Unit</th> \n    </tr></thead> \n<tbody>    <tr> \n        <th id="T_db7ac00c_6244_11e9_887c_74d4355eed37level0_row0" class="row_heading level0 row0" >0</th> \n        <td id="T_db7ac00c_6244_11e9_887c_74d4355eed37row0_col0" class="data row0 col0" >1</td> \n        <td id="T_db7ac00c_6244_11e9_887c_74d4355eed37row0_col1" class="data row0 col1" >10</td> \n        <td id="T_db7ac00c_6244_11e9_887c_74d4355eed37row0_col2" class="data row0 col2" >16</td> \n        <td id="T_db7ac00c_6244_11e9_887c_74d4355eed37row0_col3" class="data row0 col3" >13</td> \n        <td id="T_db7ac00c_6244_11e9_887c_74d4355eed37row0_col4" class="data row0 col4" >4</td> \n        <td id="T_db7ac00c_6244_11e9_887c_74d4355eed37row0_col5" class="data row0 col5" >-7</td> \n    </tr>    <tr> \n        <th id="T_db7ac00c_6244_11e9_887c_74d4355eed37level0_row1" class="row_heading level0 row1" >1</th> \n        <td id="T_db7ac00c_6244_11e9_887c_74d4355eed37row1_col0" class="data row1 col0" >-2</td> \n        <td id="T_db7ac00c_6244_11e9_887c_74d4355eed37row1_col1" class="data row1 col1" >11</td> \n        <td id="T_db7ac00c_6244_11e9_887c_74d4355eed37row1_col2" class="data row1 col2" >17</td> \n        <td id="T_db7ac00c_6244_11e9_887c_74d4355eed37row1_col3" class="data row1 col3" >14</td> \n        <td id="T_db7ac00c_6244_11e9_887c_74d4355eed37row1_col4" class="data row1 col4" >-5</td> \n        <td id="T_db7ac00c_6244_11e9_887c_74d4355eed37row1_col5" class="data row1 col5" >-8</td> \n    </tr>    <tr> \n        <th id="T_db7ac00c_6244_11e9_887c_74d4355eed37level0_row2" class="row_heading level0 row2" >2</th> \n        <td id="T_db7ac00c_6244_11e9_887c_74d4355eed37row2_col0" class="data row2 col0" >-3</td> \n        <td id="T_db7ac00c_6244_11e9_887c_74d4355eed37row2_col1" class="data row2 col1" >12</td> \n        <td id="T_db7ac00c_6244_11e9_887c_74d4355eed37row2_col2" class="data row2 col2" >18</td> \n        <td id="T_db7ac00c_6244_11e9_887c_74d4355eed37row2_col3" class="data row2 col3" >15</td> \n        <td id="T_db7ac00c_6244_11e9_887c_74d4355eed37row2_col4" class="data row2 col4" >-6</td> \n        <td id="T_db7ac00c_6244_11e9_887c_74d4355eed37row2_col5" class="data row2 col5" >9</td> \n    </tr></tbody> \n</table> '

第二个示例是在基于 Web 的 IDE Jupyter Notebooks 上运行完全相同的简化。请注意 s 的结果输出显示了所需的修改后的样式。

【讨论】:

  • 谢谢,我会探索这个解决方案,你能告诉我如何设置每列的宽度吗?
  • 尝试探索pd.describe.option() 和支持文档here。您可以通过传递pd.describe_option('width') 过滤宽度设置,并根据需要探索更改设置。如果您需要将所有设置重置为原始设置,您可以运行pd.reset_option('all')
【解决方案2】:

如果您的目标是获得带有样式单元格的 Excel 工作表,那么您仍然需要explicitly export the dataframe to an Excel file

尝试将df.style.apply(highlight_max) 替换为df.style.apply(highlight_max).to_excel('styled.xlsx')

【讨论】:

  • 在 excel 中的值就像
猜你喜欢
  • 2014-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-10
  • 1970-01-01
  • 1970-01-01
  • 2020-09-01
  • 1970-01-01
相关资源
最近更新 更多