I have a .pivot_table with margins = True.
I want to run .style.bar and .style.background_gradient on it but the problem is that margins (column totals) are also formatted and set to the maximum value so it looks non-descriptive.
I had a few ideas on how to solve this, however, none are working so far:
- Tried somehow excluding last row (the margins / total columns) from .style using subset, but failed.
- Saving the last row in a separate data frame. Removing the last row from the original data frame, applying .style and then concatenating both data frames, however, here I get an error that I cannot concatenate styled data frames.
Here is the code:
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
So the goal is to exclude the last row from formatting (All / Margins / Total column).