<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          5種Pandas圖表美化樣式匯總!

          共 3664字,需瀏覽 8分鐘

           ·

          2021-09-27 00:38

          Pandas是一種高效的數(shù)據(jù)處理庫,它以dataframe和series為基本數(shù)據(jù)類型,呈現(xiàn)出類似excel的二維數(shù)據(jù)。

          在Jupyter中,會美化Pandas的輸出。不同于IDE展示的文本形式,Jupyter可以通過CSS修改表格的樣式。

          我們在做excel表格的時候,常常會對重要數(shù)據(jù)進(jìn)行highlight,或者用不同顏色表示數(shù)據(jù)的大小。這在Pandas中也是可以實(shí)現(xiàn)的,而且非常簡潔。

          Pandas提供了DataFrame.style屬性,它會返回Styler對象,用以數(shù)據(jù)樣式的美化。

          一般的,我們需要將樣式函數(shù)作為參數(shù)傳遞到下面方法中,就可以實(shí)現(xiàn)圖表美化。

          • Styler.applymap: 作用于元素

          • Styler.apply:作用于行、列或整個表

          下面通過一些例子,具體展示常用的美化形式。

          一、高亮顯示

          為便于展示,數(shù)據(jù)示例是用的2021世界人口數(shù)量前十國家數(shù)據(jù)

          import pandas as pd
          data = pd.read_excel(r"E:\\jupyter_notebook\\2021世界人口數(shù)據(jù).xlsx")
          data

          我們先看下該表的信息:

          data.info()

          除了前兩列,其他列都為數(shù)字類型。

          現(xiàn)在對指定列的最大值進(jìn)行高亮處理:

          def highlight_max(s):
              '''
              對列最大值高亮(黃色)處理
              '''

              is_max = s == s.max()
              return ['background-color: yellow' if v else '' for v in is_max]

          data.style.apply(highlight_max,subset=['2021人口''2020人口''面積','單位面積人口','人口增幅','世界占比'])

          如果不想對元素背景高亮處理,也可以直接更改指定元素顏色,從而達(dá)到突出重點(diǎn)的目的。

          標(biāo)記單位面積人口列大于200的元素:

          def color_red(s):
              is_max = s > 200
              return ['color : red' if v else '' for v in is_max]

          data.style.apply(color_red,subset=['單位面積人口'])

          二、數(shù)據(jù)條顯示

          Excel條件格式里,有一個數(shù)據(jù)條顯示方式,用以可視化表達(dá)數(shù)據(jù)大小。

          Pandas Style方法中也有數(shù)據(jù)條的表達(dá)形式,用df.style.bar來實(shí)現(xiàn)。

          還是用前面人口數(shù)據(jù)的例子,我們來看下如何操作數(shù)據(jù)條。

          import pandas as pd
          data = pd.read_excel(r"E:\\jupyter_notebook\\2021世界人口數(shù)據(jù).xlsx")
          # 數(shù)據(jù)條顯示指定列數(shù)據(jù)大小
          data.style.bar(subset=['2021人口''2020人口'], color='#FFA500')

          三、色階顯示

          色階也就是熱力圖,它和數(shù)據(jù)條一樣,都用來表達(dá)數(shù)據(jù)大小。

          Pandas Style中色階的使用也很簡單,用df.style.background_gradient實(shí)現(xiàn)。

          import seaborn as sns

          # 使用seaborn獲取顏色
          cm = sns.light_palette("green", as_cmap=True)
          # 色階實(shí)現(xiàn)
          data.style.background_gradient(cmap=cm,subset=['2021人口''2020人口''面積','單位面積人口','人口增幅','世界占比'])

          可以通過選擇最大最小顏色比例,調(diào)節(jié)色階范圍。

          調(diào)節(jié)前:

          import seaborn as sns

          # 色階實(shí)現(xiàn),這里使用內(nèi)置色階類型,不調(diào)節(jié)顏色范圍
          data.style.background_gradient(cmap='viridis',high=0.2,low=0.1,subset=['2021人口''2020人口''面積','單位面積人口','人口增幅','世界占比'])

          調(diào)節(jié)后:

          import seaborn as sns

          # 色階實(shí)現(xiàn),這里使用內(nèi)置色階類型,調(diào)節(jié)顏色范圍
          data.style.background_gradient(cmap='viridis',high=0.5,low=0.3,subset=['2021人口''2020人口''面積','單位面積人口','人口增幅','世界占比'])

          四、百分比顯示

          有些數(shù)字需要百分比顯示才能準(zhǔn)確表達(dá),比如說人口數(shù)據(jù)里的人口增幅、世界占比。

          Pandas可以數(shù)據(jù)框中顯示百分比,通過Styler.format來實(shí)現(xiàn)。

          data.style.format("{:.2%}",subset=['人口增幅','世界占比'])

          五、標(biāo)記缺失值

          數(shù)據(jù)集中可能會存在缺失值,如果想突出顯示缺失值,該怎么操作?

          這里有好幾種常用的方法,一是用-符號替代,二是高亮顯示

          先創(chuàng)建一個帶缺失值的表,還是用人口數(shù)據(jù)。

          import pandas as pd
          import numpy as np
          data = pd.read_excel(r"E:\\jupyter_notebook\\2021世界人口數(shù)據(jù).xlsx")
          data.iloc[14] = np.nan
          data.iloc[31] = np.nan
          data.iloc[66] = np.nan
          data

          上面數(shù)據(jù)中有三個缺失值,我們用-符號替代缺失值:

          data.style.format(None, na_rep="-")

          再試試對缺失值高亮顯示:

          data.style.highlight_null(null_color='red')

          附:將樣式輸出到excel

          Pandas中的數(shù)據(jù)美化樣式不僅可以展示在notebook中,還可以輸出到excel。

          這里使用to_excel方法,并用openpyxl作為內(nèi)核

          import pandas as pd
          import numpy as np
          data = pd.read_excel(r"E:\\jupyter_notebook\\2021世界人口數(shù)據(jù).xlsx")
          data.style.background_gradient(cmap='viridis',subset=['2021人口''2020人口''面積','單位面積人口','人口增幅','世界占比']).\
                                        to_excel('style.xlsx', engine='openpyxl')
          ?

          本文參考Pandas官方文檔Styling章節(jié)

          點(diǎn)擊下方卡片進(jìn)行關(guān)注,獲取更多內(nèi)容
          點(diǎn)分享
          點(diǎn)收藏
          點(diǎn)點(diǎn)贊
          點(diǎn)在看
          瀏覽 34
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  五月婷婷激情视频 | 久久久久久黄片免费 | 四虎aⅴ网 | 国内视频精品在线播放 | 国产免费无码一区二区三区四区 |