参考:https://cloud.tencent.com/developer/article/1853145
import pandas as pd
import numpy as np
data = {
'基金名称': ['白酒基金','消费基金', '食品基金', '军工基金', '食品饮料'],
'基金规模': ['2.3','982.232','232.232', '343.2', '232.22323'],
'2018': ['-23.23', '-33.23', '0.45', '45.32', '34.34'],
'2019': ['-23.23', '23.23', '0.74', '64.32', '68.34'],
'2020': ['43.23', '62.23', '5.34', '-45.32', '34.34'],
'上任日期': ['2017-08-22 00:00:00','2017-08-22 00:00:00','2017-08-22 00:00:00','2012-05-22 00:00:00','2019-09-22 00:00:00']
}
df = pd.DataFrame(data)
df
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
|
基金名称 |
基金规模 |
2018 |
2019 |
2020 |
上任日期 |
0 |
白酒基金 |
2.3 |
-23.23 |
-23.23 |
43.23 |
2017-08-22 00:00:00 |
1 |
消费基金 |
982.232 |
-33.23 |
23.23 |
62.23 |
2017-08-22 00:00:00 |
2 |
食品基金 |
232.232 |
0.45 |
0.74 |
5.34 |
2017-08-22 00:00:00 |
3 |
军工基金 |
343.2 |
45.32 |
64.32 |
-45.32 |
2012-05-22 00:00:00 |
4 |
食品饮料 |
232.22323 |
34.34 |
68.34 |
34.34 |
2019-09-22 00:00:00 |
- ascending: False, 降序
- ascending: True, 升序
# df_consume = df.reset_index(drop=True)
df_order = df.sort_values('基金规模', ascending=False).head(10)
df_order
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
|
基金名称 |
基金规模 |
2018 |
2019 |
2020 |
1 |
消费基金 |
982.232 |
-33.23 |
23.23 |
62.23 |
3 |
军工基金 |
343.2 |
45.32 |
64.32 |
-45.32 |
2 |
食品基金 |
232.232 |
0.45 |
0.74 |
5.34 |
4 |
食品饮料 |
232.22323 |
34.34 |
68.34 |
34.34 |
0 |
白酒基金 |
2.3 |
-23.23 |
-23.23 |
43.23 |
df2 = df.style.hide_index().hide_columns(['2020'])
df2
<style type="text/css">
</style>
基金名称 |
基金规模 |
2018 |
2019 |
白酒基金 |
2.3 |
-23.23 |
-23.23 |
消费基金 |
982.232 |
-33.23 |
23.23 |
食品基金 |
232.232 |
0.45 |
0.74 |
军工基金 |
343.2 |
45.32 |
64.32 |
食品饮料 |
232.22323 |
34.34 |
68.34 |
基金名称 object
基金规模 object
2018 object
2019 object
2020 object
上任日期 object
dtype: object
# df
df[['2018','2019', '2020']] = df[['2018', '2019', '2020']].applymap(float)
df[['2018', '2019', '2020']].dtypes
2018 float64
2019 float64
2020 float64
dtype: object
axis,0列(凌冽)
axis,1行
高亮指定列的最小值、最大值
df.style.hide_index()\
.hide_columns(['上任日期'])\
.highlight_max(axis=0,subset=['2018','2019','2020'])\
.highlight_min(axis=0,subset=['2018','2019','2020'])
<style type="text/css">
#T_b02ad_row0_col3, #T_b02ad_row1_col2, #T_b02ad_row1_col4, #T_b02ad_row3_col2, #T_b02ad_row3_col4, #T_b02ad_row4_col3 {
background-color: yellow;
}
</style>
基金名称 |
基金规模 |
2018 |
2019 |
2020 |
白酒基金 |
2.3 |
-23.230000 |
-23.23 |
43.23 |
消费基金 |
982.232 |
-33.230000 |
23.23 |
62.23 |
食品基金 |
232.232 |
0.450000 |
0.74 |
5.34 |
军工基金 |
343.2 |
45.320000 |
64.32 |
-45.32 |
食品饮料 |
232.22323 |
34.340000 |
68.34 |
34.34 |
df.style.hide_index()\
.highlight_between(left=-0.2,right=30,subset=['2018','2019','2020'])
<style type="text/css">
#T_e0cc5_row1_col3, #T_e0cc5_row2_col2, #T_e0cc5_row2_col3, #T_e0cc5_row2_col4 {
background-color: yellow;
}
</style>
基金名称 |
基金规模 |
2018 |
2019 |
2020 |
上任日期 |
白酒基金 |
2.3 |
-23.230000 |
-23.230000 |
43.230000 |
2017-08-22 00:00:00 |
消费基金 |
982.232 |
-33.230000 |
23.230000 |
62.230000 |
2017-08-22 00:00:00 |
食品基金 |
232.232 |
0.450000 |
0.740000 |
5.340000 |
2017-08-22 00:00:00 |
军工基金 |
343.2 |
45.320000 |
64.320000 |
-45.320000 |
2012-05-22 00:00:00 |
食品饮料 |
232.22323 |
34.340000 |
68.340000 |
34.340000 |
2019-09-22 00:00:00 |
- 2018年的年度涨跌幅度 -24~+0 范围;
- 2019年的年度涨跌幅度 0~24 范围;
- 2020年的年度涨跌幅度 0~30 范围;
df.style.hide_index()\
.highlight_between(left=[-24,0,0],right=[0,24,30],subset=['2018','2019','2020'],axis=1)
<style type="text/css">
#T_7a3e9_row0_col2, #T_7a3e9_row1_col3, #T_7a3e9_row2_col3, #T_7a3e9_row2_col4 {
background-color: yellow;
}
</style>
基金名称 |
基金规模 |
2018 |
2019 |
2020 |
上任日期 |
白酒基金 |
2.3 |
-23.230000 |
-23.230000 |
43.230000 |
2017-08-22 00:00:00 |
消费基金 |
982.232 |
-33.230000 |
23.230000 |
62.230000 |
2017-08-22 00:00:00 |
食品基金 |
232.232 |
0.450000 |
0.740000 |
5.340000 |
2017-08-22 00:00:00 |
军工基金 |
343.2 |
45.320000 |
64.320000 |
-45.320000 |
2012-05-22 00:00:00 |
食品饮料 |
232.22323 |
34.340000 |
68.340000 |
34.340000 |
2019-09-22 00:00:00 |
df.style.hide_index()\
.hide_columns(['上任日期'])\
.highlight_max(axis=0,subset=['2018','2019','2020'], props='color:black;background-color:#99ff66')\
.highlight_min(axis=0,subset=['2018','2019','2020'], props='color:black;font-weight:bold;background-color:#ee7621')
<style type="text/css">
#T_b4f24_row0_col3, #T_b4f24_row1_col2, #T_b4f24_row3_col4 {
color: black;
font-weight: bold;
background-color: #ee7621;
}
#T_b4f24_row1_col4, #T_b4f24_row3_col2, #T_b4f24_row4_col3 {
color: black;
background-color: #99ff66;
}
</style>
基金名称 |
基金规模 |
2018 |
2019 |
2020 |
白酒基金 |
2.3 |
-23.230000 |
-23.230000 |
43.230000 |
消费基金 |
982.232 |
-33.230000 |
23.230000 |
62.230000 |
食品基金 |
232.232 |
0.450000 |
0.740000 |
5.340000 |
军工基金 |
343.2 |
45.320000 |
64.320000 |
-45.320000 |
食品饮料 |
232.22323 |
34.340000 |
68.340000 |
34.340000 |
df.style.hide_index()\
.hide_columns(['上任日期'])\
.background_gradient(cmap='Blues', subset=['基金规模'])
<style type="text/css">
#T_7a6df_row0_col1 {
background-color: #f7fbff;
color: #000000;
}
#T_7a6df_row1_col1 {
background-color: #08306b;
color: #f1f1f1;
}
#T_7a6df_row2_col1, #T_7a6df_row4_col1 {
background-color: #c9ddf0;
color: #000000;
}
#T_7a6df_row3_col1 {
background-color: #a6cee4;
color: #000000;
}
</style>
基金名称 |
基金规模 |
2018 |
2019 |
2020 |
白酒基金 |
2.3 |
-23.230000 |
-23.230000 |
43.230000 |
消费基金 |
982.232 |
-33.230000 |
23.230000 |
62.230000 |
食品基金 |
232.232 |
0.450000 |
0.740000 |
5.340000 |
军工基金 |
343.2 |
45.320000 |
64.320000 |
-45.320000 |
食品饮料 |
232.22323 |
34.340000 |
68.340000 |
34.340000 |
可以通过对 low 和 high 值的设置,可以来调节背景颜色的范围,low 和 high 分别是压缩 低端和高端的颜色范围,其数值范围一般是 0~1
df.style.hide_index()\
.hide_columns(['上任日期'])\
.background_gradient(cmap='Blues', subset=['基金规模'], low=0.3,high=0.9)
<style type="text/css">
#T_e3c74_row0_col1 {
background-color: #dceaf6;
color: #000000;
}
#T_e3c74_row1_col1 {
background-color: #4d99ca;
color: #f1f1f1;
}
#T_e3c74_row2_col1, #T_e3c74_row4_col1 {
background-color: #c7dcef;
color: #000000;
}
#T_e3c74_row3_col1 {
background-color: #b8d5ea;
color: #000000;
}
</style>
基金名称 |
基金规模 |
2018 |
2019 |
2020 |
白酒基金 |
2.3 |
-23.230000 |
-23.230000 |
43.230000 |
消费基金 |
982.232 |
-33.230000 |
23.230000 |
62.230000 |
食品基金 |
232.232 |
0.450000 |
0.740000 |
5.340000 |
军工基金 |
343.2 |
45.320000 |
64.320000 |
-45.320000 |
食品饮料 |
232.22323 |
34.340000 |
68.340000 |
34.340000 |
规模在20以下的,颜色最浅,规模70以上的,颜色最深,20~70亿之间的,颜色渐变
df.style.hide_index()\
.hide_columns(['上任日期'])\
.background_gradient(subset=['基金规模'], cmap='Blues',vmin=20,vmax=900)
<style type="text/css">
#T_4eb5a_row0_col1 {
background-color: #f7fbff;
color: #000000;
}
#T_4eb5a_row1_col1 {
background-color: #08306b;
color: #f1f1f1;
}
#T_4eb5a_row2_col1, #T_4eb5a_row4_col1 {
background-color: #c8dcf0;
color: #000000;
}
#T_4eb5a_row3_col1 {
background-color: #a0cbe2;
color: #000000;
}
</style>
基金名称 |
基金规模 |
2018 |
2019 |
2020 |
白酒基金 |
2.3 |
-23.230000 |
-23.230000 |
43.230000 |
消费基金 |
982.232 |
-33.230000 |
23.230000 |
62.230000 |
食品基金 |
232.232 |
0.450000 |
0.740000 |
5.340000 |
军工基金 |
343.2 |
45.320000 |
64.320000 |
-45.320000 |
食品饮料 |
232.22323 |
34.340000 |
68.340000 |
34.340000 |
df.style.hide_index()\
.hide_columns(['上任日期'])\
.bar(subset=['2018','2020'],color=['#99ff66','#ee7621'])
<style type="text/css">
#T_ffef4_row0_col2 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,#99ff66 12.7%, transparent 12.7%);
}
#T_ffef4_row0_col4 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,#ee7621 82.3%, transparent 82.3%);
}
#T_ffef4_row1_col2, #T_ffef4_row3_col4 {
width: 10em;
height: 80%;
}
#T_ffef4_row1_col4, #T_ffef4_row3_col2 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,#ee7621 100.0%, transparent 100.0%);
}
#T_ffef4_row2_col2 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,#ee7621 42.9%, transparent 42.9%);
}
#T_ffef4_row2_col4 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,#ee7621 47.1%, transparent 47.1%);
}
#T_ffef4_row4_col2 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,#ee7621 86.0%, transparent 86.0%);
}
#T_ffef4_row4_col4 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,#ee7621 74.1%, transparent 74.1%);
}
</style>
基金名称 |
基金规模 |
2018 |
2019 |
2020 |
白酒基金 |
2.3 |
-23.230000 |
-23.230000 |
43.230000 |
消费基金 |
982.232 |
-33.230000 |
23.230000 |
62.230000 |
食品基金 |
232.232 |
0.450000 |
0.740000 |
5.340000 |
军工基金 |
343.2 |
45.320000 |
64.320000 |
-45.320000 |
食品饮料 |
232.22323 |
34.340000 |
68.340000 |
34.340000 |
def max_value(x, color='red'):
return np.where(x == np.nanmax(x.to_numpy()),
f"color: {color};background-color:yellow", None)
df.style.hide_index()\
.hide_columns(['上任日期'])\
.apply(max_value,axis=0,subset=['2018','2019','2020'])
<style type="text/css">
#T_83dd0_row1_col4, #T_83dd0_row3_col2, #T_83dd0_row4_col3 {
color: red;
background-color: yellow;
}
</style>
基金名称 |
基金规模 |
2018 |
2019 |
2020 |
白酒基金 |
2.3 |
-23.23 |
-23.23 |
43.23 |
消费基金 |
982.232 |
-33.23 |
23.23 |
62.23 |
食品基金 |
232.232 |
0.45 |
0.74 |
5.34 |
军工基金 |
343.2 |
45.32 |
64.32 |
-45.32 |
食品饮料 |
232.22323 |
34.34 |
68.34 |
34.34 |
def color_returns(val):
if float(val) > 0:
color = '#EE7621' # light red
elif float(val) < 0:
color = '#99ff66' # light green '#99ff66'
else:
color = '#FFFAFA' # ligth gray
return f'background-color: {color}'
df.style.hide_index()\
.hide_columns(['上任日期'])\
.applymap(color_returns,subset=pd.IndexSlice[1:5,['2018','2019','2020']])
<style type="text/css">
#T_854d7_row1_col2, #T_854d7_row3_col4 {
background-color: #99ff66;
}
#T_854d7_row1_col3, #T_854d7_row1_col4, #T_854d7_row2_col2, #T_854d7_row2_col3, #T_854d7_row2_col4, #T_854d7_row3_col2, #T_854d7_row3_col3, #T_854d7_row4_col2, #T_854d7_row4_col3, #T_854d7_row4_col4 {
background-color: #EE7621;
}
</style>
基金名称 |
基金规模 |
2018 |
2019 |
2020 |
白酒基金 |
2.3 |
-23.23 |
-23.23 |
43.23 |
消费基金 |
982.232 |
-33.23 |
23.23 |
62.23 |
食品基金 |
232.232 |
0.45 |
0.74 |
5.34 |
军工基金 |
343.2 |
45.32 |
64.32 |
-45.32 |
食品饮料 |
232.22323 |
34.34 |
68.34 |
34.34 |
df.style.hide_index()\
.hide_columns(['上任日期'])\
.applymap(color_returns, subset=pd.IndexSlice[3:2])
<style type="text/css">
</style>
基金名称 |
基金规模 |
2018 |
2019 |
2020 |
白酒基金 |
2.3 |
-23.23 |
-23.23 |
43.23 |
消费基金 |
982.232 |
-33.23 |
23.23 |
62.23 |
食品基金 |
232.232 |
0.45 |
0.74 |
5.34 |
军工基金 |
343.2 |
45.32 |
64.32 |
-45.32 |
食品饮料 |
232.22323 |
34.34 |
68.34 |
34.34 |
保存样式
style1 = df.style.hide_index()\
.highlight_min(axis=1,subset=['2018','2019','2020'],props='color:black;background-color:#99ff66')\
.highlight_max(axis=1,subset=['2018','2019','2020'],props='color:black;background-color:#ee7621')\
.highlight_null(props='color:white;background-color:darkblue')
style1
<style type="text/css">
#T_464b6_row0_col2, #T_464b6_row0_col3, #T_464b6_row1_col2, #T_464b6_row2_col2, #T_464b6_row3_col4, #T_464b6_row4_col2, #T_464b6_row4_col4 {
color: black;
background-color: #99ff66;
}
#T_464b6_row0_col4, #T_464b6_row1_col4, #T_464b6_row2_col4, #T_464b6_row3_col3, #T_464b6_row4_col3 {
color: black;
background-color: #ee7621;
}
</style>
基金名称 |
基金规模 |
2018 |
2019 |
2020 |
上任日期 |
白酒基金 |
2.3 |
-23.23 |
-23.23 |
43.23 |
2017-08-22 00:00:00 |
消费基金 |
982.232 |
-33.23 |
23.23 |
62.23 |
2017-08-22 00:00:00 |
食品基金 |
232.232 |
0.45 |
0.74 |
5.34 |
2017-08-22 00:00:00 |
军工基金 |
343.2 |
45.32 |
64.32 |
-45.32 |
2012-05-22 00:00:00 |
食品饮料 |
232.22323 |
34.34 |
68.34 |
34.34 |
2019-09-22 00:00:00 |
使用样式
df_fund_1 = df[['2018','2019','2020']]
df_fund_1.style.use(style1.export())
<style type="text/css">
#T_f491f_row0_col0, #T_f491f_row0_col1, #T_f491f_row1_col0, #T_f491f_row2_col0, #T_f491f_row3_col2, #T_f491f_row4_col0, #T_f491f_row4_col2 {
color: black;
background-color: #99ff66;
}
#T_f491f_row0_col2, #T_f491f_row1_col2, #T_f491f_row2_col2, #T_f491f_row3_col1, #T_f491f_row4_col1 {
color: black;
background-color: #ee7621;
}
</style>
|
2018 |
2019 |
2020 |
0 |
-23.23 |
-23.23 |
43.23 |
1 |
-33.23 |
23.23 |
62.23 |
2 |
0.45 |
0.74 |
5.34 |
3 |
45.32 |
64.32 |
-45.32 |
4 |
34.34 |
68.34 |
34.34 |
def max_value(x, color='red'):
return np.where(x == np.nanmax(x.to_numpy()),
f"color: {color};background-color:yellow", None)
df.style.hide_index()\
.hide_columns(['上任日期'])\
.apply(max_value,axis=0,subset=['2018','2019','2020'])\
.to_excel('style_export.xlsx',engine = 'openpyxl')