0

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.

2 Answers2

0

df is a DataFrame class object.

df.style returns a Styler class object.

df.style.some_style_function() also returns a Styler class object.

When the last item returned in a Jupyter Notebook is a Styler object which has an _repr_html_() method, that is called and the resulting HTML is displayed in the cell.

When you call display(df) you are essentially calling df._repr_html_() and that is obviously not the same as df.style.some_styler_function()._repr_html_() becuase they are different python objects.

Additionally df._repr_html_() has a column width trimming option meaning the cells have ... at the end which prevents the HTML from properly rendering. You can turn that off if you review the documentation for pandas.options.

Attack68
  • 4,437
  • 1
  • 20
  • 40
  • Thank you for your time, I could clearly understand what is the difference between DataFrame and Styler after your explanation, however when I added the `pd.set_option('display.max_colwidth', None)`, I got the following results:[Result](https://i.stack.imgur.com/8AgIi.png), which is basically the extended previously codes, and have not complied yet. @Attack68 – Mohammad Badri Ahmadi Aug 25 '21 at 01:20
  • @Attack69 hey man, I am not sure if it something normal or rude, I am pretty new to this platform. I just know you are extremely expert on this area, so was wondering if you can help me with my new [question](https://stackoverflow.com/questions/68941912/how-to-apply-pandas-set-option-python-to-pandas-style-objects). If you find this rude, lmk and I will stop bothering you. You have helped me more than enough so far, so i'm more than grateful for your help. Cheers – Mohammad Badri Ahmadi Aug 26 '21 at 16:07
0

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.