13、表格样式创建

 

 

In [ ]:
'''
表格样式创建

表格视觉样式:Dataframe.style → 返回pandas.Styler对象的属性,具有格式化和显示Dataframe的有用方法

样式创建:
① Styler.applymap:elementwise → 按元素方式处理Dataframe
② Styler.apply:column- / row- / table-wise → 按行/列处理Dataframe

'''
In [2]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
In [3]:
# 样式

df = pd.DataFrame(np.random.randn(10,4),columns=['a','b','c','d'])
sty = df.style
print(sty,type(sty))
# 查看样式类型

sty
# 显示样式
 
<pandas.io.formats.style.Styler object at 0x000002B4D97A5E10> <class 'pandas.io.formats.style.Styler'>
Out[3]:
 
 abcd
0 0.994717 -1.76931 -1.04364 0.662782
1 0.507953 -0.751604 -3.20665 -0.00316762
2 -1.67345 0.777769 1.52595 -0.68124
3 1.39059 0.303007 1.36888 -1.40817
4 0.590499 1.25339 -1.843 -1.19178
5 0.907079 -0.752169 0.646874 -0.104434
6 -0.282473 -0.13618 0.425609 0.256449
7 0.317963 -0.188314 -0.395865 2.02621
8 -1.62159 1.22982 2.43284 -1.12136
9 0.73547 0.64652 -0.455337 0.923736
In [4]:
# 按元素处理样式:style.applymap()

def color_neg_red(val):
    if val < 0:
        color = 'red'
    else:
        color = 'black'
    return('color:%s' % color)
df.style.applymap(color_neg_red)

# 创建样式方法,使得小于0的数变成红色
# style.applymap() → 自动调用其中的函数
Out[4]:
 
 abcd
0 0.994717 -1.76931 -1.04364 0.662782
1 0.507953 -0.751604 -3.20665 -0.00316762
2 -1.67345 0.777769 1.52595 -0.68124
3 1.39059 0.303007 1.36888 -1.40817
4 0.590499 1.25339 -1.843 -1.19178
5 0.907079 -0.752169 0.646874 -0.104434
6 -0.282473 -0.13618 0.425609 0.256449
7 0.317963 -0.188314 -0.395865 2.02621
8 -1.62159 1.22982 2.43284 -1.12136
9 0.73547 0.64652 -0.455337 0.923736
In [5]:
# 按行/列处理样式:style.apply()

def highlight_max(s):
    is_max = s == s.max()
    #print(is_max) # 布尔型索引
    lst = []
    for v in is_max:
        if v:
            lst.append('background-color: yellow')
        else:
            lst.append('')
    return(lst)
df.style.apply(highlight_max, axis = 0, subset = ['b','c'])
# 创建样式方法,每列最大值填充黄色
# axis:0为列,1为行,默认为0
# subset:索引
Out[5]:
 
 abcd
0 0.994717 -1.76931 -1.04364 0.662782
1 0.507953 -0.751604 -3.20665 -0.00316762
2 -1.67345 0.777769 1.52595 -0.68124
3 1.39059 0.303007 1.36888 -1.40817
4 0.590499 1.25339 -1.843 -1.19178
5 0.907079 -0.752169 0.646874 -0.104434
6 -0.282473 -0.13618 0.425609 0.256449
7 0.317963 -0.188314 -0.395865 2.02621
8 -1.62159 1.22982 2.43284 -1.12136
9 0.73547 0.64652 -0.455337 0.923736
In [7]:
# 样式索引、切片

df.style.apply(highlight_max, axis = 1, 
               subset = pd.IndexSlice[2:5,['b', 'd']])
# 通过pd.IndexSlice[]调用切片
# 也可:df[2:5].style.apply(highlight_max, subset = ['b', 'd']) → 先索引行再做样式
Out[7]:
 
 abcd
0 0.994717 -1.76931 -1.04364 0.662782
1 0.507953 -0.751604 -3.20665 -0.00316762
2 -1.67345 0.777769 1.52595 -0.68124
3 1.39059 0.303007 1.36888 -1.40817
4 0.590499 1.25339 -1.843 -1.19178
5 0.907079 -0.752169 0.646874 -0.104434
6 -0.282473 -0.13618 0.425609 0.256449
7 0.317963 -0.188314 -0.395865 2.02621
8 -1.62159 1.22982 2.43284 -1.12136
9 0.73547 0.64652 -0.455337 0.923736
In [ ]:
 
posted @ 2019-04-16 21:20  慕沁  阅读(468)  评论(0编辑  收藏  举报