我有一个.pivot_表,边距=True。在
我想跑。样式栏还有。style.background_渐变色但问题是,页边距(列总计)也被格式化并设置为最大值,因此它看起来不是描述性的。在
关于如何解决这个问题,我有一些想法,但是,到目前为止,没有一个是有效的:
代码如下:
import pandas as pd
import numpy as np
import seaborn as sns
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, fill_value = 0)
df = (df.style.background_gradient(subset = 'large', cmap = sns.light_palette('red', as_cmap = True))
.background_gradient(subset = 'small', cmap = sns.light_palette('green', as_cmap = True)))
df
所以我们的目标是从格式化中排除最后一行(All/Margins/Total column)。在
您必须更加明确一些,但是您可以使用
get_level_values
和pd.IndexSlice
来完成所需的工作相关问题 更多 >
编程相关推荐