Pandas style-dataframe has multi-index columns misaligned

268 Views Asked by At

I need to style a Pandas dataframe that has a multi-index column arrangement. As an example of my dataframe, consider:

df = pd.DataFrame([[True for a in range(12)]])
df.columns = pd.MultiIndex.from_tuples([(a,b,c) for a in ['Tim','Sarah'] for b in ['house','car','boat'] for c in ['new','used']])
df

This displays the multi-index columns just as I would expect it to, and is easy to read: enter image description here

However, when I convert it to a style dataframe:

df.style

The column headers suddenly shift and it becomes confusing to figure out where each level begins and ends:

enter image description here

How can I undo this, to return it to the more readable left-justified setup? I looked through about 10 other posts on SO but none addressed this issue.

Update

My primary product is a .to_excel() output, and it displays correctly, I just learned. However this issue is still open for solutions.

0

There are 0 best solutions below