I am formatting a pandas dataframe with styler to highlight columns and format numbers. I also want to apply multi-index for more clear, pleasant and easy to read. Since I apply Styler to subset of columns it does not work work with the multi-index.
Example:
arrays = [np.hstack([['One']*2, ['Two']*2]) , ['A', 'B', 'C', 'D']]
columns = pd.MultiIndex.from_arrays(arrays)
data = pd.DataFrame(np.random.randn(5, 4), columns=list('ABCD'))
data.columns = columns
import seaborn as sns
cm = sns.light_palette("green", as_cmap=True)
data.style.background_gradient(cmap=cm, subset=['A'])
Is there a way to subset the columns so the styler can work. According to the below sources this is implemented but there is not examples so it is hard to me to understand how to apply it:
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.formats.style.Styler.html
https://github.com/pandas-dev/pandas/issues/11655
thank you !
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…