用Python在Jupyter Noteb中实现基于多个容差值的单个数据帧单元的彩色编码

2024-03-29 14:22:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我想根据特定的公差值对数据帧的各个单元格进行颜色编码,我尝试了多种方法,但由于某些原因,我无法理解为什么Jupyter笔记本没有对我的数据帧进行颜色编码。你知道吗

熊猫造型:https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html

我有一个下表,我愿意遍历行:[行,1:6]。你知道吗

|Parameter| Method 1 | Method 2 | Method 3 | Method 4 | Method 5 | Weighing % |

|Parameter 1| **99.6     | 100      | 100      | 99.8     | 100**      | 25         |

|Parameter 2| **0.4      | 0        | 0        | 0.2      | 0**        | 5          |

|Parameter 3| **100      | 100      | 100      | 100      | 100**      | 5          |

|Parameter 4| **1.3      | 1.2      | 1.1      | 0.9      | 1.4**      | 2.5        |

每个参数都有公差值,应将单元格中的值与之进行比较。根据比较,将为单元格选择某种颜色。 单元格值和公差值:

values_for_comparison = [cell value, tolerance_1st_min, tolerance_1st_max, tolerance_2nd_min, tolerance_2nd_max, tolerance_3rd_min, tolerance_3rd_max, tolerance_4th_min, tolerance_4th_max, tolerance_5th_min]

在主程序中:

o = 0
rows_length_table3_df = table3_df.shape[0]
p = 1
columns_length_table3 = all_table1s.shape[1]
q = 1

while o < rows_length_table3_df:
    while q < columns_length_table3:
        value = table3_df.iloc[o, p]
        values_for_comparison = [value,
                                 tolerance_values_conclusions_tables_df.iloc[o, 1],
                                 tolerance_values_conclusions_tables_df.iloc[o, 2],
                                 tolerance_values_conclusions_tables_df.iloc[o, 3],
                                 tolerance_values_conclusions_tables_df.iloc[o, 4],
                                 tolerance_values_conclusions_tables_df.iloc[o, 5],
                                 tolerance_values_conclusions_tables_df.iloc[o, 6],
                                 tolerance_values_conclusions_tables_df.iloc[o, 7],
                                 tolerance_values_conclusions_tables_df.iloc[o, 8],
                                 tolerance_values_conclusions_tables_df.iloc[o, 9]]
        table3_df.style.applymap(comparison_conclusions.colors_tables(values_for_comparison), subset = [table3_df.iloc[o, p]])
        p += 1
        q += 1
    p = 1
    q = 1
    o += 1
    values_for_comparison = [None] * 10

exec("table3_df.to_csv(os.path.join(conclusions_folder_path, r'{0}_{1}_Visits_Conclusions_Statistics_Table3.csv'))".format(name_of_the_reference_point_folder, total_number_of_visits_at_the_reference_point))
print(table3_df)

基于公差值(values_for_comparison list[1:])定义单个单元格(val)的颜色:

def colors_tables(values_for_comparison):
    print('colors_tables')
    def colors_tables_by_val(val, values_for_comparison):
        print('colors_tables_by_val')
        if values_for_comparison[2] < values_for_comparison[1]:
            if val <= values_for_comparison[1] and val >= values_for_comparison[2]:
                color = 'forestgreen'
            elif val < values_for_comparison[3] and val >= values_for_comparison[4]:
                color = 'greenyellow'
            elif val < values_for_comparison[5] and val >= values_for_comparison[6]:
                color = 'yellow'
            elif val < values_for_comparison[7] and val >= values_for_comparison[8]:
                color = 'orange'
            elif val < values_for_comparison[9]:
                color = 'red'
            else:
                color = 'white'
            print(color)

            return 'background-color: {}'.format(color)

        else:
            if val >= 0 and val <= values_for_comparison[2]:
                color = 'forestgreen'
            elif val > values_for_comparison[3] and val <= values_for_comparison[4]:
                color = 'greenyellow'
            elif val > values_for_comparison[5] and val <= values_for_comparison[6]:
                color = 'yellow'
            elif val > values_for_comparison[7] and val <= values_for_comparison[8]:
                color = 'orange'
            elif val > values_for_comparison[9]:
                color = 'red'
            else:
                color = 'white'
            print(color)

            return 'background-color: {}'.format(color)
    val = values_for_comparison[0]
    colors_tables_by_val(val, values_for_comparison)
    return colors_tables_by_val

这些颜色不会应用于主程序中的任何单元格,但会打印这些颜色。你知道吗

我应该如何正确地编写代码?至少这部分代码:

table3_df.style.applymap(comparison_conclusions.colors_tables(values_for_comparison), subset = [table3_df.iloc[o, p]])

更新20190708

我将代码改为following,但仍然没有为table3_df应用任何颜色。但颜色是印刷的:

o = 0
rows_length_table3_df = table3_df.shape[0]
p = 1
columns_length_table3 = all_table1s.shape[1]
q = 1

while o < rows_length_table3_df:
    while q < columns_length_table3:
        values_for_comparison = [tolerance_values_conclusions_tables_df.iloc[o, 1],
                                 tolerance_values_conclusions_tables_df.iloc[o, 2],
                                 tolerance_values_conclusions_tables_df.iloc[o, 3],
                                 tolerance_values_conclusions_tables_df.iloc[o, 4],
                                 tolerance_values_conclusions_tables_df.iloc[o, 5],
                                 tolerance_values_conclusions_tables_df.iloc[o, 6],
                                 tolerance_values_conclusions_tables_df.iloc[o, 7],
                                 tolerance_values_conclusions_tables_df.iloc[o, 8],
                                 tolerance_values_conclusions_tables_df.iloc[o, 9]]
        value = table3_df.iloc[o, p]
        def colors_tables(value):
            val = value
            print('colors_tables')
            if values_for_comparison[1] < values_for_comparison[0]:
                if val <= values_for_comparison[0] and val >= values_for_comparison[1]:
                    color = 'forestgreen'
                elif val < values_for_comparison[2] and val >= values_for_comparison[3]:
                    color = 'greenyellow'
                elif val < values_for_comparison[4] and val >= values_for_comparison[5]:
                    color = 'yellow'
                elif val < values_for_comparison[6] and val >= values_for_comparison[7]:
                    color = 'orange'
                elif val < values_for_comparison[8]:
                    color = 'red'
                else:
                    color = 'white'
                print(color)

                return 'background-color: %s' % color

            else:
                if val >= 0 and val <= values_for_comparison[1]:
                    color = 'forestgreen'
                elif val > values_for_comparison[2] and val <= values_for_comparison[3]:
                    color = 'greenyellow'
                elif val > values_for_comparison[4] and val <= values_for_comparison[5]:
                    color = 'yellow'
                elif val > values_for_comparison[6] and val <= values_for_comparison[7]:
                    color = 'orange'
                elif val > values_for_comparison[8]:
                    color = 'red'
                else:
                    color = 'white'
                print(color)

                return 'background-color: %s' % color
        table3_df.style.applymap(colors_tables(value), subset = (o, p))
        p += 1
        q += 1
    p = 1
    q = 1
    o += 1
    values_for_comparison = [None] * 9

exec("table3_df.to_csv(os.path.join(conclusions_folder_path, r'{0}_{1}_Visits_Conclusions_Statistics_Table3.csv'))".format(name_of_the_reference_point_folder, total_number_of_visits_at_the_reference_point))
print(table3_df)

Tags: anddffortablesvaltolerancecomparisoncolor