Search code examples
pythonpandasdataframejupyter-notebookpandas-styles

Jupyter notebook shows a different output when calling display for Pandas DataFrames (Python). It does not compile HTML codes within each cell


I am using Jupyter Notebook and Pandas DataFrame to look through some 3D and 4D matrices. I am using what @Attack68 has replied in this question.

The problem is, when I use only one jupyter cell for making a DataFrame, without calling "display" function, it works just fine, and will run HTML codes perfectly. You can see an example here:

import pandas as pd
data = [
    [[1]],
    [[1.0,2.0],[2.0,4.0],[8.0,3.0],[9.0,7.0]],
    [[0.487],[1.532],[1.544],[1.846]],
    [[3.0]],
    [[3.0]],
    [[-1]],
]
    
df = pd.DataFrame([
    [(pd.DataFrame(x)
        .style
        .hide_index()
        .hide_columns()
        .set_table_attributes('class="matrix"')
        .to_html()
     ) for x in data]
], dtype="object")
df.style.set_table_styles([
    {"selector": ".matrix", "props": "position: relative;"},
    {"selector": ".matrix:before, .matrix:after", 
     "props":  'content: ""; position: absolute; top: 0; border: 1px solid #000; width: 6px; height: 100%;'
    },
    {"selector": ".matrix:before", "props": "left: -0px; border-right: -0;"},
    {"selector": ".matrix:after", "props": "right: -0px; border-left: 0;"}
])

Which will result in : Intended_Result (my reputation is not enough to use inline image. Sorry about inconvenience)

However, the problem is, as soon as I add display function to display the table, it stops compiling the HTML codes and showing the matrices itself, and returns only the codes instead as the DataFrame cells.

import pandas as pd
data = [
    [[1]],
    [[1.0,2.0],[2.0,4.0],[8.0,3.0],[9.0,7.0]],
    [[0.487],[1.532],[1.544],[1.846]],
    [[3.0]],
    [[3.0]],
    [[-1]],
]
    
df = pd.DataFrame([
    [(pd.DataFrame(x)
        .style
        .hide_index()
        .hide_columns()
        .set_table_attributes('class="matrix"')
        .to_html()
     ) for x in data]
], dtype="object")
df.style.set_table_styles([
    {"selector": ".matrix", "props": "position: relative;"},
    {"selector": ".matrix:before, .matrix:after", 
     "props":  'content: ""; position: absolute; top: 0; border: 1px solid #000; width: 6px; height: 100%;'
    },
    {"selector": ".matrix:before", "props": "left: -0px; border-right: -0;"},
    {"selector": ".matrix:after", "props": "right: -0px; border-left: 0;"}
])
display(df)

You can see the results here: Wrong_Results

Thanks.


Solution

  • First of all, I cannot appreciate enough @Attack69 helps. The following solution is solely done by the help of him. I just made one simple edit, after I understood the difference between Styler and DataFrame perfectly explained by @Attack69.

    The solution seems to be pretty simply, we only need to use display(df.style) instead of display(df)

    Again, I have no idea how long it would take me to come up with the solution with out the help of @Attack68. Thanks for helping me out with my basic questions. I am pretty much new in this area.