1

In this example

import numpy as np
import pandas as pd
from itertools import product

df = pd.DataFrame({'Value':np.arange(10001,10007)}, 
    index=list(product(*[['A','B'],['a','b','c']])))
df.index = pd.MultiIndex.from_tuples(df.index, names=['Caps','Lower'])

df; df.style.format("{:,}")

when df is output to a Jupyter notebook, the values in column Caps are aligned to the top. However, when df.style.format("{:,}") is output in a Jupyter notebook, the values in column Caps are centered. Is there a way of forcing those values to be at the top? (i.e. they should be in the same line as a and not in the line as b).

2 Answers 2

1

You can use set_table_styles with selector:

df.style.format("{:,}").set_table_styles([{'selector':'th',
                                           'props':[('vertical-align','top')]}])

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

By default, pandas displays the index labels a row below the column labels. For display purposes, you can get them on the same horizontal line by resetting the index.

df.reset_index()

   Caps Lower Value
0   A   a   10001
1   A   b   10002
2   A   c   10003
3   B   a   10004
4   B   b   10005
5   B   c   10006

Comments

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.