Pandas style-dataframe has multi-index columns misaligned

Viewed 135

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

Can anyone help me 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.

TIA.

UPDATE 12/9: My primary product is a .to_excel() output, and it displays correctly, I just learned. So while this issue is still open for solutions, I am not in urgent need of a solution, but am in search of one nonetheless. Thanks.

0 Answers
Related