简体   繁体   English

是否可以从熊猫数据透视表的样式(子集)中排除总行(边距)

[英]Is it possible to exclude the total row (margins) from styling (subset) in a pandas pivot table

So for example I ran a .pivot_table call with margins=True. 因此,例如,我运行了一个带有margins = True的.pivot_table调用。 Now I want to run .style.bar on it but the total of each column always sets the max value for the bar so it kinda looks nondescript. 现在,我要在其上运行.style.bar,但每列的总和始终设置该条的最大值,因此看起来有点难以描述。 Any way around this? 可以解决吗?

import pandas as pd
import numpy as np

df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo",
                      "bar", "bar", "bar", "bar"],
                "B": ["one", "one", "one", "two", "two",
                      "one", "one", "two", "two"],
                "C": ["small", "large", "large", "small",
                      "small", "large", "small", "small",
                     "large"],
                "D": [1, 2, 2, 3, 3, 4, 5, 6, 7]})
df = df.pivot_table(values='D', index=['A','B'], columns=['C'], aggfunc=np.sum, margins=True)
df = df.style.bar(subset=['small','large','All'],align='mid',color='red')
from IPython.core.display import HTML
HTML(df.render())        

Basically 7, 6, and 13 should have been the maxes (second row of pivot table) 基本上7、6和13应该是最大值(数据透视表的第二行)

Much easier than I expected. 比我预期的容易得多。 Just needed to call this before running the style command 只需要在运行style命令之前调用它

df = df.drop(index='All', axis=0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM