Tuesday 30 April 2019

df.style.apply centers multiindex values in display

When I run

import pandas as pd
from IPython.display import display
df = pd.DataFrame('a',index=pd.MultiIndex.from_product([[0,1]]*3),columns=['A'])
display(df)
display(df.style.apply(lambda x: ['']*len(x),axis=1))

in Jupyter, I get the following displays (unfortunately I cannot upload screenshots)

0 0 0 a
    1 a
  1 0 a
    1 a
1 0 0 a
    1 a
  1 0 a
    1 a

-

    0 a
  0  
    1 a
0
    0 a
  1  
    1 a
    0 a
  0  
    1 a
1
    0 a
  1  
    1 a

1: How do I prevent that change?

2: Since I actually don't dislike the second format completely: How can I fix the backgrounds in the multiindex, so that the boundaries of the groups in each multiindex level become visually clear?

(PS: In my actual code, the lambda returns 'background: green' for some rows)



from df.style.apply centers multiindex values in display

No comments:

Post a Comment