pandas按条件筛选和设置表格样式

pandas 按条件筛选和设置表格样式。

https://pandas.pydata.org/docs/user_guide/indexing.html#boolean-indexing

https://pandas.pydata.org/docs/reference/style.html#style

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#%%
import pandas as pd

# df 数据来自 《pandas合并pd.concat-pd.merge》

#%%
print('\n*** 计算: 对比数据 与 基础 的差 标记: _pd ***')
cols = ['rmse1', 'r_2_1', 'rmse2', 'r_2_2', 'rmse3', 'r_2_3']

for col in cols:
df[col] = df[col] * 100
df[col+'p'] = df[col+'p'] * 100
df[col+'_pd'] = df[col] - df[col+'p']

df.head(10)
#%%

print('\n*** 计算: LSTM模型 与 RNN 的差, 标记: _md ***')
for idx, row in df.iterrows():
if row.model=='LSTM':
on1 = df.model == 'RNN'
on2 = df.steps == row.steps
on3 = df.seed == row.seed
on4 = df.epochs == row.epochs
on5 = df.out_size == row.out_size

pk = df[on1 & on2 & on3 & on4 & on5]
pk_idx = pk.index[0]

for col in cols:
md = df.loc[idx, col] - df.loc[pk_idx, col]
df.loc[idx, col+'_md'] = md

# %%
colsp = ['rmse1_pd', 'r_2_1_pd', 'rmse2_pd', 'r_2_2_pd', 'rmse3_pd', 'r_2_3_pd']
colsm = ['rmse1_md', 'r_2_1_md', 'rmse2_md', 'r_2_2_md', 'rmse3_md', 'r_2_3_md']

cols1 = ['rmse1_md', 'rmse2_md', 'rmse3_md']
cols2 = ['r_2_1_md', 'r_2_2_md', 'r_2_3_md']

format_dict = {'delta': '{0:.2%}'}
for col in colsp: format_dict[col] = '{0:.2%}'
for col in colsm: format_dict[col] = '{0:.2%}'


def color_map1(val):
color = 'white'
if val < 0: color = 'lightgreen'
return f'background-color: {color}'
def color_map2(val):
color = '#FFFFFF'
if val > 0: color = 'lightblue'
return f'background-color: {color}'

df.style.format(format_dict)\
.highlight_between(left=-0.05,right=0.05, subset=colsp)\
.applymap(color_map1, subset=['rmse1_md', 'rmse2_md', 'rmse3_md'])\
.applymap(color_map2, subset=['r_2_1_md', 'r_2_2_md', 'r_2_3_md'])