我想根据特定的公差值对数据框的各个单元进行颜色编码,并且我尝试了多种方法,但是由于某些原因,我不知道为什么Jupyter Notebook不对数据框进行颜色编码。
熊猫样式: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
我将代码更改为以下代码,但仍未为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)