多列样式 pandas pivot_table

Styling pandas pivot_table with multiple columns

我有一个 DataFrame,我从中创建了一个数据透视表 table 并格式化了结果。

index1 = ["New Category"]
columns1 = []
values1 = ["Net Sales"]

pivot = pd.pivot_table(df, index=index1, values=values1, columns=columns1,aggfunc=np.sum, fill_value="", margins=True)
pivot.style.format({ ('Net Sales'):'${0:,.2f}'})

<style type="text/css"></style><table id="T_c34e9_"> <thead> <tr> <th class="blank level0" >&nbsp;</th> <th class="col_heading level0 col0" >Net Sales</th> </tr> <tr> <th class="index_name level0" >New Category</th> <th class="blank col0" >&nbsp;</th> </tr> </thead> <tbody> <tr> <th id="T_c34e9_level0_row0" class="row_heading level0 row0" >Beans</th> <td id="T_c34e9_row0_col0" class="data row0 col0" >,741.99</td> </tr> <tr> <th id="T_c34e9_level0_row1" class="row_heading level0 row1" >Coffee</th> <td id="T_c34e9_row1_col0" class="data row1 col0" >7,041.77</td> </tr> <tr> <th id="T_c34e9_level0_row2" class="row_heading level0 row2" >Gift Card</th> <td id="T_c34e9_row2_col0" class="data row2 col0" >0.00</td> </tr> <tr> <th id="T_c34e9_level0_row3" class="row_heading level0 row3" >Hot Choc</th> <td id="T_c34e9_row3_col0" class="data row3 col0" >,213.55</td> </tr> <tr> <th id="T_c34e9_level0_row4" class="row_heading level0 row4" >Merchandise</th> <td id="T_c34e9_row4_col0" class="data row4 col0" >2,122.79</td> </tr> <tr> <th id="T_c34e9_level0_row5" class="row_heading level0 row5" >Package Food</th> <td id="T_c34e9_row5_col0" class="data row5 col0" >,923.21</td> </tr> <tr> <th id="T_c34e9_level0_row6" class="row_heading level0 row6" >Prepared Food</th> <td id="T_c34e9_row6_col0" class="data row6 col0" >,770.69</td> </tr> <tr> <th id="T_c34e9_level0_row7" class="row_heading level0 row7" >Shipping</th> <td id="T_c34e9_row7_col0" class="data row7 col0" >,088.49</td> </tr> <tr> <th id="T_c34e9_level0_row8" class="row_heading level0 row8" >Tea</th> <td id="T_c34e9_row8_col0" class="data row8 col0" >,927.17</td> </tr> <tr> <th id="T_c34e9_level0_row9" class="row_heading level0 row9" >All</th> <td id="T_c34e9_row9_col0" class="data row9 col0" >3,929.66</td> </tr> </tbody></table>

格式有效。当我尝试添加列时,格式停止工作:

index1 = ["New Category"]
columns1 = ["year"]
values1 = ["Net Sales"]

pivot = pd.pivot_table(df, index=index1, values=values1, columns=columns1, aggfunc=np.sum, fill_value="", margins=True)
pivot.style.format({ ('Net Sales'):'${0:,.2f}'})

<style type="text/css"></style><table id="T_52828_"> <thead> <tr> <th class="blank level0" >&nbsp;</th> <th class="col_heading level0 col0" colspan="5">Net Sales</th> </tr> <tr> <th class="index_name level1" >year</th> <th class="col_heading level1 col0" >2018</th> <th class="col_heading level1 col1" >2019</th> <th class="col_heading level1 col2" >2020</th> <th class="col_heading level1 col3" >2021</th> <th class="col_heading level1 col4" >All</th> </tr> <tr> <th class="index_name level0" >New Category</th> <th class="blank col0" >&nbsp;</th> <th class="blank col1" >&nbsp;</th> <th class="blank col2" >&nbsp;</th> <th class="blank col3" >&nbsp;</th> <th class="blank col4" >&nbsp;</th> </tr> </thead> <tbody> <tr> <th id="T_52828_level0_row0" class="row_heading level0 row0" >Beans</th> <td id="T_52828_row0_col0" class="data row0 col0" >3344.250000</td> <td id="T_52828_row0_col1" class="data row0 col1" >24658.720000</td> <td id="T_52828_row0_col2" class="data row0 col2" >39929.330000</td> <td id="T_52828_row0_col3" class="data row0 col3" >31809.690000</td> <td id="T_52828_row0_col4" class="data row0 col4" >99741.990000</td> </tr> <tr> <th id="T_52828_level0_row1" class="row_heading level0 row1" >Coffee</th> <td id="T_52828_row1_col0" class="data row1 col0" >7039.850000</td> <td id="T_52828_row1_col1" class="data row1 col1" >50371.570000</td> <td id="T_52828_row1_col2" class="data row1 col2" >58534.170000</td> <td id="T_52828_row1_col3" class="data row1 col3" >51096.180000</td> <td id="T_52828_row1_col4" class="data row1 col4" >167041.770000</td> </tr> <tr> <th id="T_52828_level0_row2" class="row_heading level0 row2" >Gift Card</th> <td id="T_52828_row2_col0" class="data row2 col0" ></td> <td id="T_52828_row2_col1" class="data row2 col1" ></td> <td id="T_52828_row2_col2" class="data row2 col2" >75.000000</td> <td id="T_52828_row2_col3" class="data row2 col3" >25.000000</td> <td id="T_52828_row2_col4" class="data row2 col4" >100.000000</td> </tr> <tr> <th id="T_52828_level0_row3" class="row_heading level0 row3" >Hot Choc</th> <td id="T_52828_row3_col0" class="data row3 col0" >2405.920000</td> <td id="T_52828_row3_col1" class="data row3 col1" >7868.880000</td> <td id="T_52828_row3_col2" class="data row3 col2" >5536.500000</td> <td id="T_52828_row3_col3" class="data row3 col3" >2402.250000</td> <td id="T_52828_row3_col4" class="data row3 col4" >18213.550000</td> </tr> <tr> <th id="T_52828_level0_row4" class="row_heading level0 row4" >Merchandise</th> <td id="T_52828_row4_col0" class="data row4 col0" >2919.410000</td> <td id="T_52828_row4_col1" class="data row4 col1" >32250.860000</td> <td id="T_52828_row4_col2" class="data row4 col2" >51714.060000</td> <td id="T_52828_row4_col3" class="data row4 col3" >35238.460000</td> <td id="T_52828_row4_col4" class="data row4 col4" >122122.790000</td> </tr> <tr> <th id="T_52828_level0_row5" class="row_heading level0 row5" >Package Food</th> <td id="T_52828_row5_col0" class="data row5 col0" >190.400000</td> <td id="T_52828_row5_col1" class="data row5 col1" >2116.750000</td> <td id="T_52828_row5_col2" class="data row5 col2" >6943.500000</td> <td id="T_52828_row5_col3" class="data row5 col3" >4672.560000</td> <td id="T_52828_row5_col4" class="data row5 col4" >13923.210000</td> </tr> <tr> <th id="T_52828_level0_row6" class="row_heading level0 row6" >Prepared Food</th> <td id="T_52828_row6_col0" class="data row6 col0" >489.730000</td> <td id="T_52828_row6_col1" class="data row6 col1" >7905.560000</td> <td id="T_52828_row6_col2" class="data row6 col2" >15263.650000</td> <td id="T_52828_row6_col3" class="data row6 col3" >15111.750000</td> <td id="T_52828_row6_col4" class="data row6 col4" >38770.690000</td> </tr> <tr> <th id="T_52828_level0_row7" class="row_heading level0 row7" >Shipping</th> <td id="T_52828_row7_col0" class="data row7 col0" ></td> <td id="T_52828_row7_col1" class="data row7 col1" >51.880000</td> <td id="T_52828_row7_col2" class="data row7 col2" >840.610000</td> <td id="T_52828_row7_col3" class="data row7 col3" >196.000000</td> <td id="T_52828_row7_col4" class="data row7 col4" >1088.490000</td> </tr> <tr> <th id="T_52828_level0_row8" class="row_heading level0 row8" >Tea</th> <td id="T_52828_row8_col0" class="data row8 col0" >85.050000</td> <td id="T_52828_row8_col1" class="data row8 col1" >742.120000</td> <td id="T_52828_row8_col2" class="data row8 col2" >1078.000000</td> <td id="T_52828_row8_col3" class="data row8 col3" >1022.000000</td> <td id="T_52828_row8_col4" class="data row8 col4" >2927.170000</td> </tr> <tr> <th id="T_52828_level0_row9" class="row_heading level0 row9" >All</th> <td id="T_52828_row9_col0" class="data row9 col0" >16474.610000</td> <td id="T_52828_row9_col1" class="data row9 col1" >125966.340000</td> <td id="T_52828_row9_col2" class="data row9 col2" >179914.820000</td> <td id="T_52828_row9_col3" class="data row9 col3" >141573.890000</td> <td id="T_52828_row9_col4" class="data row9 col4" >463929.660000</td> </tr> </tbody></table>

格式不会传递到子列。

我也试过这些,没有运气

pivot.style.format({ ('Net Sales','year'):'${0:,.2f}'})
pivot.style.format({ ('Net Sales','2018'):'${0:,.2f}'})

如何设置子栏的样式?

根据此处的文档(参见示例中的 table):https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html#Formatting-Values

df.style.format(precision=0, na_rep='MISSING', thousands=" ",
            formatter={('Decision Tree', 'Tumour'): "{:.2f}",
                       ('Regression', 'Non-Tumour'): lambda x: "$ {:,.1f}".format(x*-1e6)
                      })

您可以看到 'year' 不是您需要应用格式的列;是个别年份“2019”、“2020”等

如果您需要将它应用于所有数字,您可以使用:

pivot.style.format('{:.2f}')

根据此处的示例:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.io.formats.style.Styler.format.html#pandas-io-formats-style-styler-format

或者您可以列出每个列组合(最好在将它们传递给函数之前创建这些元组):

pivot.style.format({ ('Net Sales','2018'): "{:.2f}", ('Net Sales','2019'):'${0:,.2f}', ('Net Sales','2020'):'${0:,.2f}', ('Net Sales','2021'):'${0:,.2f}', ('Net Sales','All'):'${0:,.2f}'})

事实证明,年份不是字符串,而是数字。所以 pivot.style.format({ ('Net Sales',2018):'${0:,.2f}'}) 作品。在之前的尝试中这对我不起作用的原因是列中的值并非都是浮点数;有些是字符串。这是因为我使用 fill_value="" 来填充缺失值。将其更改为 fill_value=0 解决了问题。