怎么给pandas.DataFrame锦上添花?
共 5000字,需浏览 10分钟
·
2021-06-24 21:42
更新一下好久没动的Python数据分析基础专辑。
平日看到的pandas.DataFrame数据是这个样子的~平淡无奇,索然无味,读了本文后,可以「这样子」了~
「这样子」~
或者「这样子」~
目录
![](https://filescdn.proginn.com/d112c81b1a8d182b464b9114ab71eaf5/7bc87359acb13a67e7498729f658c4cc.webp)
以上个性化的设置主要用到pd.DataFrame.style的如下属性:
['apply', 'applymap', 'background_gradient', 'bar', 'caption', 'cell_ids', 'clear', 'columns', 'ctx', 'data', 'env', 'export', 'format', 'from_custom_template', 'hidden_columns', 'hidden_index', 'hide_columns', 'hide_index', 'highlight_max', 'highlight_min', 'highlight_null', 'index', 'loader', 'na_rep', 'pipe', 'precision', 'render', 'set_caption', 'set_na_rep', 'set_precision', 'set_properties', 'set_table_attributes', 'set_table_styles', 'set_uuid', 'table_attributes', 'table_styles', 'template', 'to_excel', 'use', 'uuid', 'where']
本文介绍部分~
1 - style.applymap着色符合条件的每个元素
import pandas as pd
import numpy as np
np.random.seed(24)
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
df = pd.concat(
[df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))], axis=1)
# 添加缺省值
df.iloc[3, 3] = np.nan
df.iloc[0, 2] = np.nan
# style.applymap着色符合条件的每个元素
def color_negative_red(val):
"""
小于0的元素上红色、反之上蓝色
"""
color = '#c72e29' if val < 0 else '#01a2d9'
return 'color: %s' % color
s = df.style.applymap(color_negative_red)
s
![](https://filescdn.proginn.com/d6a5b9041d901f4afda64ec6aed61474/65665cb5086e1a7bac536af53cb5229e.webp)
2 - apply着色符合条件的column-/row-/table-wise
def highlight_max(s):
'''
对DataFrame的Seris中最大值上绿色
'''
is_max = s == s.max()
return ['background-color: #74C476' if v else '' for v in is_max]
df.style.apply(highlight_max)
![](https://filescdn.proginn.com/db592c1465d39192d0fdcbfdc390601d/17577fad1594d3a91bc35324cbf1bb32.webp)
3 - style.applymap、apply联合使用
#.号连接即可
df.style.\
applymap(color_negative_red).\
apply(highlight_max)
![](https://filescdn.proginn.com/b2eb1ec478f7c2b423c2fd95a54483fc/09ab9d8d05fa37094490ded12cac8c5b.webp)
4 - style.background_gradient设置背景填充色
import seaborn as sns
s = df.style.background_gradient(cmap='Set2_r')
s
![](https://filescdn.proginn.com/bc867490b7530acf11e1eb400e7bdf85/630a2dd503d739fc999ae071d7eb1a45.webp)
5 - style.bar绘制Series柱状图
df.style.bar(subset=['A', 'B'], align='mid', color=['#dc2624', '#649E7D'])
![](https://filescdn.proginn.com/e5297414d353856302fbc9af60aaf3e8/d8c490b3a5e2833fd54e00c9de623702.webp)
import pandas as pd
from IPython.display import HTML
# Test series
test1 = pd.Series([-100, -60, -30, -20], name='All Negative')
test2 = pd.Series([10, 20, 50, 100], name='All Positive')
test3 = pd.Series([-10, -5, 0, 90], name='Both Pos and Neg')
head = """
<table>
<thead>
<th>Align</th>
<th>All Negative</th>
<th>All Positive</th>
<th>Both Neg and Pos</th>
</thead>
</tbody>
"""
aligns = ['left', 'zero', 'mid']
for align in aligns:
row = "<tr><th>{}</th>".format(align)
for series in [test1, test2, test3]:
s = series.copy()
s.name = ''
row += "<td>{}</td>".format(
s.to_frame().style.bar(align=align,
color=['#dc2624', '#649E7D'],
width=100).render()) #testn['width']
row += '</tr>'
head += row
head += """
</tbody>
</table>"""
HTML(head)
![](https://filescdn.proginn.com/edcc9941fbbb7118fd2c4d0a2e20e0c2/64972c261d2b93e329f55b838091442e.webp)
6 - to_excel导出个性化结果到excel中
df.style.\
applymap(color_negative_red).\
apply(highlight_max).\
to_excel('styled.xlsx', engine='openpyxl')
![](https://filescdn.proginn.com/b55d1c0110da00994d2e077dde016a71/5d91be58b2e7cb465d117a1060d94d2f.webp)
7 - 更多设置
https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html
相关阅读: