Pandas style doesn't work with Google colab
Asked Answered
L

4

12

I'm facing a strange behavior with Google colab and pandas style. When applying a style to a dataframe on google colab, some of the basic styling is messed up: the table becomes smaller and more condensed, the highlight of every other row disappears, and the hover highlight of rows stops working.

I'm attaching a side-by-side picture of two screenshots: one from a regular Jupiter notebook, in which things are working fine, and another one from Google colab - in which styling messes up things.

The code is extremely simple:

df = pd.DataFrame(range(5)) # create a data frame

df                          # in a new cell - just show the dataframe

df.style.highlight_max()    # again, in a new cell. Works in Jupyter notebook, 
                            # doesn't work well on Google Colab. 

Any help would be appreciated.

enter image description here

Lavinialavinie answered 8/6, 2020 at 5:53 Comment(7)
I'm struggling through this right now. solutions to do things like add borders to cells in a pandas DataFrame simply do not work in CoLab. Have you found a solution?Paring
@BreaksSoftware I don't have any new information about this. Sorry.Lavinialavinie
I get the expected output if I try this on the internal version of colab, but not the public version. I'll see if I can forward this bug to the Colab team. Thanks.Murillo
would find the correct formatting useful as well! thanksStrawboard
A workaround would be to add custom style. Example: w3resource.com/python-exercises/pandas/style/…Brookner
Reported at github.com/googlecolab/colabtools/issues/1687Orle
@Brookner Custom styles don't work in colab eitherSauncho
E
1

I don't think this is pandas styler. All pandas does is render HTML which works well in Notebook or normal browsers. (Jupyter has its own set of CSS that it applies to rendered tables). Google colab will have their own set of CSS.

I suspect the hierarchy of colab's default CSS is not as dominant as Jupyters. Maybe file an issue with them?

Embree answered 22/2, 2021 at 19:5 Comment(0)
F
0

It is the issue with html rendering of Colab but you could try something like this to fix it although it does not give the exact same results as notebook does.

df.style.highlight_max(axis=0).set_properties( **{'width': '30px'},**{'text-align': 'center'})

Output:

center aligned style

Forenamed answered 6/4, 2021 at 8:52 Comment(0)
C
0

It worked for me:

import pandas as pd
df = pd.read_csv('/content/drive/My Drive/Colab Notebooks/teste.csv')
df.head(100).style.highlight_max(axis=0).set_properties( **{'color': 'red'})

however it does not work with this feature:

%load_ext google.colab.data_table
Cohe answered 14/7, 2021 at 18:8 Comment(0)
A
0

From: https://github.com/googlecolab/colabtools/issues/1687

Colab only applies the 'dataframe' styling to HTML table elements with the dataframe classname. The regular dataframe adds the dataframe class but the styler one does not.

The best approach is probably to explicitly add the dataframe class:

df.style.highlight_max().set_table_attributes('class="dataframe"')

Aecium answered 1/11, 2023 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.