mmngreco
3/2/2018 - 8:35 AM

4 differents ways to display tables side by side on jupyter with python.

4 differents ways to display tables side by side on jupyter with python.

source : https://stackoverflow.com/questions/38783027/jupyter-notebook-display-two-pandas-tables-side-by-side

METHOD 1

from IPython.display import display, HTML
CSS = """
.output {
flex-direction: row;
}
"""

HTML('<style>{}</style>'.format(CSS))

display(df)

METHOD 2

from IPython.display import display_html
def display_side_by_side(*args):
    html_str=''
    for df in args:
        html_str += df.to_html()
        html_str += '\t'
    display_html(html_str.replace('table','table style="display:inline"'), raw=True)

METHOD 2

from IPython.display import display_html
def display_side_by_side(*args):
    html_str=''
    for df in args:
        html_str += df.to_html()
        html_str += '\t'
    display_html(html_str.replace('table','table style="display:inline"'), raw=True)

METHOD 3

from IPython.display import display, HTML

def multi_column_df_display(list_dfs, cols=3):
    html_table = "<table style='width:100%; border:0px'>{content}</table>"
    html_row = "<tr style='border:0px'>{content}</tr>"
    html_cell = "<td style='width:{width}%;vertical-align:top;border:0px'>"\
                "{{content}}</td>"
    html_cell = html_cell.format(width=100/cols)
    cells = [ html_cell.format(content=df.to_html()) for df in list_dfs ]
    cells += (cols - (len(list_dfs)%cols)) * [html_cell.format(content="")] # pad
    rows = [html_row.format(content="".join(cells[i:i+cols])) for i in range(0, len(cells), cols)]
    display(HTML(html_table.format(content="".join(rows))))

METHOD 4

class display(object):
    """Display HTML representation of multiple objects"""
    template = """<div style="float: left; padding: 10px;">
    <p style='font-family:"Courier New", Courier, monospace'>{0}</p>{1}
    </div>"""

    def __init__(self, *args):
        self.args = args

    def _repr_html_(self):
        return '\n'.join(self.template.format(a, eval(a)._repr_html_())
                     for a in self.args)

    def __repr__(self):
       return '\n\n'.join(a + '\n' + repr(eval(a))
                       for a in self.args)