天天看點

Pandas:利用Styler對象設定Series、Dataframe在Jupyter Notebook中的輸出樣式(2)——内置樣式

目前

pandas

版本為:1.2.5。

Styler

對象内置樣式概述

pandas

資料結構在

Jupyter Notebook

中以表格形式呈現。這些表格的格式化依賴于

pandas

中的

Styler

對象。

Dateframe.style

屬性傳回值為

Styler

對象。

Styler

對象通過生成CSS樣式進行格式化。

Styler

對象内置了一系列樣式方法。這些方法的傳回值大部分還是

Styler

對象,

Styler

對象支援鍊式調用,這樣就可以将多種樣式疊加在一起。

  • highlight_null

    :高亮顯示

    null

    值。
  • highlight_min

    :高亮顯示最小值。
  • highlight_max

    :高亮顯示最大值。
  • background_gradient

    :依賴

    matplotlib

    ,支援

    seaborn

    ,以熱力圖(色階)形式顯示數值大小。
  • bar

    :以資料條形式顯示數值大小。

下面以案例的形式示範内置樣式方法的使用,所有案例以以下案例為基礎。

案例基礎

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

score = pd.read_csv('./student_score.csv',encoding = 'gbk')
score
           
Pandas:利用Styler對象設定Series、Dataframe在Jupyter Notebook中的輸出樣式(2)——内置樣式

highlight_null

:高亮顯示

null

highlight_null

方法的簽名如下:

highlight_null(null_color: str = 'red', subset: Union[Hashable, NoneType, Sequence[Union[Hashable, NoneType]]] = None) -> 'Styler'
           

highlight_null

方法具有兩個參數:

  • null_color

    :指定

    null

    值的高亮顔色。類型為字元串,預設為紅色。
  • subset

    :指定作用範圍(子集)。

highlight_null

方法的傳回值為

Styler

對象。

案例:高亮紅色顯示

null

# 構造null值
score.iloc[1, 2] = np.nan
score.style.highlight_null()
           
Pandas:利用Styler對象設定Series、Dataframe在Jupyter Notebook中的輸出樣式(2)——内置樣式

案例:高亮黃色顯示

null

# 構造null值
score.iloc[1, 2] = np.nan
score.style.highlight_null(null_color='yellow')
           
Pandas:利用Styler對象設定Series、Dataframe在Jupyter Notebook中的輸出樣式(2)——内置樣式

highlight_min

:高亮顯示最小值

highlight_min

方法的簽名如下:

highlight_min(subset=None, color: str = 'yellow', axis: Union[str, int, NoneType] = 0) -> 'Styler'
           

highlight_min

方法具有兩個參數:

  • color

    :指定最小值的高亮顔色。類型為字元串,預設為黃色。
  • subset

    :指定作用範圍(子集)。
  • axis

    :軸方向,即設定最小值的比較範圍。預設值為 即按列方向比較,

    1

    為按行方向比較。

highlight_min

方法的傳回值為

Styler

對象。

案例:高亮黃色顯示每列最小值

score.style.highlight_min()
           
Pandas:利用Styler對象設定Series、Dataframe在Jupyter Notebook中的輸出樣式(2)——内置樣式

案例:高亮黃色顯示每行最小值

score.style.highlight_min(axis=1)
           
Pandas:利用Styler對象設定Series、Dataframe在Jupyter Notebook中的輸出樣式(2)——内置樣式

案例:高亮黃色顯示所有元素中的最小值

score.style.highlight_min(axis=None)
           
Pandas:利用Styler對象設定Series、Dataframe在Jupyter Notebook中的輸出樣式(2)——内置樣式

案例:高亮紅色顯示某列中的最小值

score.style.highlight_min(axis=0,subset='高數',color='red')
           
Pandas:利用Styler對象設定Series、Dataframe在Jupyter Notebook中的輸出樣式(2)——内置樣式

highlight_max

:高亮顯示最大值

highlight_max

方法的簽名如下:

highlight_max(subset=None, color: str = 'yellow', axis: Union[str, int, NoneType] = 0) -> 'Styler'
           

highlight_max

方法的參數、傳回值與

highlight_min

方法類似,不再贅述。

background_gradient

:以熱力圖(色階)形式顯示數值大小

background_gradient

方法依賴

matplotlib

,支援

seaborn

background_gradient

方法的簽名為:

background_gradient(cmap='PuBu', low: float = 0, high: float = 0, axis: Union[str, int, NoneType] = 0, subset=None, text_color_threshold: float = 0.408, vmin: Union[float, NoneType] = None, vmax: Union[float, NoneType] = None) -> 'Styler'
           

background_gradient

方法的參數為:

  • cmap

    Matplotlib

    顔色映射(colormap)。類型為字元串或

    colormap

    ,預設值為’PuBu’
  • low

    :最小顔色範圍。類型為浮點值。預設為0。
  • high

    :最大顔色範圍。類型為浮點值。預設為0。
  • axis

    :軸方向,即設定值的比較範圍。預設值為 即按列方向比較,

    1

    為按行方向比較。
  • subset

    :指定作用範圍(子集)。
  • text_color_threshold

    :文本的亮度門檻值,用于輔助文本在不同背景色下的顯示。類型為浮點值或整數,取值範圍為

    0-1

    , 為全部文本都為暗色,

    1

    為全部文本都為亮色。
  • vmin

    :colormap中的最小值。類型為浮點數,預設值為資料中的最小值。
  • vmax

    :colormap中的最大值。類型為浮點數,預設值為資料中的最大值。

background_gradient

方法的傳回值為

Styler

對象。

案例:

background_gradient()

方法預設樣式

score.style.background_gradient()
           
Pandas:利用Styler對象設定Series、Dataframe在Jupyter Notebook中的輸出樣式(2)——内置樣式

案例:使用matplotlib内置colormap高亮顯示

score.style.background_gradient(cmap='autumn_r')
           
Pandas:利用Styler對象設定Series、Dataframe在Jupyter Notebook中的輸出樣式(2)——内置樣式

案例:調整colormap顔色範圍

score.style.background_gradient(cmap='autumn_r',high=0.5,low=0.5)
           
Pandas:利用Styler對象設定Series、Dataframe在Jupyter Notebook中的輸出樣式(2)——内置樣式

bar

:以資料條形式顯示數值大小。

bar

方法的簽名為:

bar(subset=None, axis: Union[str, int, NoneType] = 0, color='#d65f5f', width: float = 100, align: str = 'left', vmin: Union[float, NoneType] = None, vmax: Union[float, NoneType] = None) -> 'Styler' 
           

bar

方法的參數為:

  • width

    :最大值占單元格寬度的百分比。類型為浮點值,取值範圍為

    0-100

    。預設為

    100

  • axis

    :軸方向,即設定資料條的比較範圍。預設值為 即按列方向比較,

    1

    為按行方向比較。
  • subset

    :指定作用範圍(子集)。
  • color

    :資料條顔色。類型為字元串或二進制數組/清單。預設值為

    '#d65f5f'

    • 字元串:所有單元格使用同一種顔色。
    • 二進制數組/清單:分别定義負值和正值資料條顔色,即

      (負值顔色,正值顔色)

  • align :資料條的對齊方式。取值範圍為

    {'left', 'zero',' mid'}

    ,預設值為

    'left'

    • 'left'

      :最小值位于單元格的左側。
    • 'zero'

      : 值位于單元格的中央。
    • 'mid'

      (max-min)/2

      值位于單元格的中央。如果所有值均為正值, 值位于單元格左側;如果所有值均為負值, 值位于單元格右側。
  • vmin

    :資料條(左側)最小值。類型為浮點數,預設值為資料中的最小值。
  • vmax

    :資料條(右側)最大值。類型為浮點數,預設值為資料中的最大值。

bar

方法的傳回值為

Styler

對象。

案例:

bar()

方法預設樣式

score.style.bar()
           
Pandas:利用Styler對象設定Series、Dataframe在Jupyter Notebook中的輸出樣式(2)——内置樣式

案例:

bar()

方法

align

參數示範

score.style.bar(align='mid')
           
Pandas:利用Styler對象設定Series、Dataframe在Jupyter Notebook中的輸出樣式(2)——内置樣式

參考文獻

https://pandas.pydata.org/docs/user_guide/style.html

繼續閱讀