簡體   English   中英

行方式格式化pandas數據幀

[英]Format pandas dataframe row wise

我有以下數據框,並希望將其轉換為HTML

            Limit        Status     Warning      3M AVG
VAR1        1.20         1.21216    1.11         1.21235
VAR2        0.82         0.63075    0.75         0.593295
VAR3        0.38         0.376988   0.35         0.376988
VAR4        0.17         0.126987   0.14         0.12461

我想逐行格式化這個數據幀,以便:

  1. 如果Status超過Warning ,整行將突出顯示為黃色,如果超過Limit則整行將突出顯示為紅色
  2. VAR2VAR3具有“{:。2%}”格式, VAR1VAR4具有“{:。2f}”

我已經挖掘了pandas文檔並嘗試了幾種方法,但我無法完成上述所有任務

如果您能提供幫助,我將不勝感激,因為我認為對於許多大熊貓用戶而言,行格式化數據幀是一項挑戰。

編輯1:我嘗試了以下代碼:

df=df.transpose()    
df.style.format("{:.2%}").format({"VAR1":"{:.2f},"VAR4":"{:.2f}"})

注意:通過轉置數據框,可以更輕松地完成所有任務,但我無法將其轉換回原始形狀,因為它是樣式器。

我認為您可以使用自定義樣式功能執行您想要的操作:

def color(row):
    if row.Status >= row.Limit:
        return ['background-color: red'] * len(row)
    elif row.Status >= row.Warning:
        return ['background-color: yellow'] * len(row)
    return [''] * len(row)

df.style.apply(color, axis=1)

在此輸入圖像描述

但是,您仍然需要為此添加自定義數字格式。

要獲取此代碼的HTML代碼,請使用render方法:

df.style.apply(color, axis=1).render()

 <style type="text/css" > #T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow0_col0 { background-color: red; } #T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow0_col1 { background-color: red; } #T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow0_col2 { background-color: red; } #T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow0_col3 { background-color: red; } #T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow2_col0 { background-color: yellow; } #T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow2_col1 { background-color: yellow; } #T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow2_col2 { background-color: yellow; } #T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow2_col3 { background-color: yellow; }</style> <table id="T_e61b55e0_cef5_11e8_9f07_68f72880acdc" > <thead> <tr> <th class="blank level0" ></th> <th class="col_heading level0 col0" >Limit</th> <th class="col_heading level0 col1" >Status</th> <th class="col_heading level0 col2" >Warning</th> <th class="col_heading level0 col3" >3M AVG</th> </tr></thead> <tbody> <tr> <th id="T_e61b55e0_cef5_11e8_9f07_68f72880acdclevel0_row0" class="row_heading level0 row0" >VAR1</th> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow0_col0" class="data row0 col0" >1.2</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow0_col1" class="data row0 col1" >1.21216</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow0_col2" class="data row0 col2" >1.11</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow0_col3" class="data row0 col3" >1.21235</td> </tr> <tr> <th id="T_e61b55e0_cef5_11e8_9f07_68f72880acdclevel0_row1" class="row_heading level0 row1" >VAR2</th> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow1_col0" class="data row1 col0" >0.82</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow1_col1" class="data row1 col1" >0.63075</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow1_col2" class="data row1 col2" >0.75</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow1_col3" class="data row1 col3" >0.593295</td> </tr> <tr> <th id="T_e61b55e0_cef5_11e8_9f07_68f72880acdclevel0_row2" class="row_heading level0 row2" >VAR3</th> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow2_col0" class="data row2 col0" >0.38</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow2_col1" class="data row2 col1" >0.376988</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow2_col2" class="data row2 col2" >0.35</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow2_col3" class="data row2 col3" >0.376988</td> </tr> <tr> <th id="T_e61b55e0_cef5_11e8_9f07_68f72880acdclevel0_row3" class="row_heading level0 row3" >VAR4</th> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow3_col0" class="data row3 col0" >0.17</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow3_col1" class="data row3 col1" >0.126987</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow3_col2" class="data row3 col2" >0.14</td> <td id="T_e61b55e0_cef5_11e8_9f07_68f72880acdcrow3_col3" class="data row3 col3" >0.12461</td> </tr></tbody> </table> 

我有同樣的問題,並研究pandas.io.formats.style.Styler類中的format函數的實現,並實現了類似的行方式函數:

def format_row_wise(styler, formatter):
    for row, row_formatter in formatter.items():
        row_num = styler.index.get_loc(row)

        for col_num in range(len(styler.columns)):
            styler._display_funcs[(row_num, col_num)] = row_formatter
    return styler

示例

df = pandas.DataFrame(
    {
        'Limit': [1.20, 0.82, 0.38, 0.17], 
        'Status': [1.21216, 0.63075, 0.376988, 0.126987], 
        'Warning': [1.11, 0.75, 0.35, 0.14], 
        '3M AVG': [1.21235, 0.593259, 0.376988, 0.12461]
    }, 
    index=['VAR1', 'VAR2', 'VAR3', 'VAR4']
)
formatters = {"VAR1":lambda x: f"{x:.2f}", "VAR4": lambda x: f"{x:.2f}"}
styler = format_row_wise(df.style, formatters)
styler.render()

這對我有用:)

注意

  • 我只實現了dict格式化程序!
  • 格式必須作為函數給出(這里:lambda)

希望這能讓你走上正確的道路......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM