熊猫数据框样式:根据格式列突出显示一些单元格

时间:2020-09-27 21:00:54

标签: python pandas dataframe

问题描述

我有一个DataFrame,其中最后一列是格式列。该列的目的是包含DataFrame行的格式。 这是此类数据框的示例:

df = pd.DataFrame({'ID': [1, 24, 31, 37],
                   'Status': ['to analyze', 'to analyze','to analyze','analyzed'],
                   'priority' : ['P1','P1','P2','P1'],
                   'format' : ['n;y;n','n;n;n','n;y;y','y;n;y']}

每个df ['format']行均包含一个字符串,该字符串应作为列表(拆分时)使用,以给出该行的格式。

符号含义:

  • n表示“不突出显示”
  • y表示“以黄色突出显示”

df ['format']。to_list()[0] ='n; y; n'表示例如:

  • n:第一列ID项目“ 1”未突出显示
  • y:第二列状态项“要分析”要突出显示
  • n:第三列优先项“ P1”未突出显示

所以预期结果是:

Expected outcome

我尝试过的事情

我尝试使用df.format获取包含所需格式的列表列表。这是我的代码:

import pandas as pd
import numpy as np

def highlight_case(df):
    list_of_format_lists = []
    for format_line in df['format']:
        format_line_list = format_line.split(';')
        format_list = []
        for form in format_line_list:
            if 'y' in form:
                format_list.append('background-color: yellow')
            else:
                format_list.append('')
        list_of_format_lists.append(format_list)
    list_of_format_lists = list(map(list, zip(*list_of_format_lists)))#transpose
    print(list_of_format_lists)
    return list_of_format_lists


highlight_style = highlight_case(df)
df.style.apply(highlight_style)

它不起作用,我得到以下输出:

TypeError                                 Traceback (most recent call last)
c:\python38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    343             method = get_real_method(obj, self.print_method)
    344             if method is not None:
--> 345                 return method()
    346             return None
    347         else:

c:\python38\lib\site-packages\pandas\io\formats\style.py in _repr_html_(self)
    191         Hooks into Jupyter notebook rich display system.
    192         """
--> 193         return self.render()
    194 
    195     @doc(NDFrame.to_excel, klass="Styler")

c:\python38\lib\site-packages\pandas\io\formats\style.py in render(self, **kwargs)
    538         * table_attributes
    539         """
--> 540         self._compute()
    541         # TODO: namespace all the pandas keys
    542         d = self._translate()

c:\python38\lib\site-packages\pandas\io\formats\style.py in _compute(self)
    623         r = self
    624         for func, args, kwargs in self._todo:
--> 625             r = func(self)(*args, **kwargs)
    626         return r
    627 

c:\python38\lib\site-packages\pandas\io\formats\style.py in _apply(self, func, axis, subset, **kwargs)
    637         data = self.data.loc[subset]
    638         if axis is not None:
--> 639             result = data.apply(func, axis=axis, result_type="expand", **kwargs)
    640             result.columns = data.columns
    641         else:

c:\python38\lib\site-packages\pandas\core\frame.py in apply(self, func, axis, raw, result_type, args, **kwds)
   7543             kwds=kwds,
   7544         )
-> 7545         return op.get_result()
   7546 
   7547     def applymap(self, func) -> "DataFrame":

c:\python38\lib\site-packages\pandas\core\apply.py in get_result(self)
    142         # dispatch to agg
    143         if is_list_like(self.f) or is_dict_like(self.f):
--> 144             return self.obj.aggregate(self.f, axis=self.axis, *self.args, **self.kwds)
    145 
    146         # all empty

c:\python38\lib\site-packages\pandas\core\frame.py in aggregate(self, func, axis, *args, **kwargs)
   7353         axis = self._get_axis_number(axis)
   7354 
-> 7355         relabeling, func, columns, order = reconstruct_func(func, **kwargs)
   7356 
   7357         result = None

c:\python38\lib\site-packages\pandas\core\aggregation.py in reconstruct_func(func, **kwargs)
     74 
     75     if not relabeling:
---> 76         if isinstance(func, list) and len(func) > len(set(func)):
     77 
     78             # GH 28426 will raise error if duplicated function names are used and

TypeError: unhashable type: 'list'

1 个答案:

答案 0 :(得分:2)

由于格式是针对每一行进行编码的,因此按行chmod +x script.js有意义:

apply

输出:

enter image description here