3

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.

3
  • 1
    It doesn't for me with you df. Works just fine. Commented Dec 5, 2020 at 16:50
  • @SergedeGossondeVarennes thank you, but I don't know what to do with that! I'm on python 3.8.3, and this was in a Jupyter Notebook. You? Commented Dec 5, 2020 at 17:28
  • 1
    Same version of python, pandas 1.05 and in a notebook as well. Commented Dec 5, 2020 at 17:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.