1三、表格樣式建立

 

 

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]:
 
  a b c d
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]:
 
  a b c d
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]:
 
  a b c d
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]:
 
  a b c d
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 [ ]:
相關文章
相關標籤/搜索