Two Jupyter laptops display two pandas tables

I have two pandas frames and would like to display them in a Jupyter laptop.

Doing something like:

display(df1) display(df2) 

Shows them below:

enter image description here

I would like to have a second data frame to the right of the first. There is a similar question , but it seems that the person is satisfied either by merging them into a single data frame to show the difference between them.

This will not work for me. In my case, dataframes can represent completely different (disparate elements), and their size can be different. Thus, my main goal is to save space.

+64
pandas ipython-notebook jupyter-notebook
source share
9 answers

You can override the CSS of the output code. The default is flex-direction: column . Try replacing it with row . Here is an example:

 import pandas as pd import numpy as np from IPython.display import display, HTML CSS = """ .output { flex-direction: row; } """ HTML('<style>{}</style>'.format(CSS)) 

Jupyter Image

You could, of course, customize the CSS as you like.

If you want to target only one output of a cell, try using the selector :nth-child() . For example, this code will only change the CSS of the output of the 5th cell in the notebook:

 CSS = """ div.cell:nth-child(5) .output { flex-direction: row; } """ 
+59
source share

I ended up writing a function that can do this:

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

Usage example:

 df1 = pd.DataFrame(np.arange(12).reshape((3,4)),columns=['A','B','C','D',]) df2 = pd.DataFrame(np.arange(16).reshape((4,4)),columns=['A','B','C','D',]) display_side_by_side(df1,df2,df1) 

enter image description here

+76
source share

Starting with pandas 0.17.1 visualization of DataFrames can be directly modified using pandas 0.17.1 methods.

To display two DataFrames side by side, you must use set_table_attributes with the argument "style='display:inline'" as suggested in the ntg answer . This will return two Styler objects. To display aligned data frames, simply pass their combined HTML representation through the display_html method from IPython.

Using this method is also easier to add other style options. How to add a signature as required here :

 import numpy as np import pandas as pd from IPython.display import display_html df1 = pd.DataFrame(np.arange(12).reshape((3,4)),columns=['A','B','C','D',]) df2 = pd.DataFrame(np.arange(16).reshape((4,4)),columns=['A','B','C','D',]) df1_styler = df1.style.set_table_attributes("style='display:inline'").set_caption('Caption table 1') df2_styler = df2.style.set_table_attributes("style='display:inline'").set_caption('Caption table 2') display_html(df1_styler._repr_html_()+df2_styler._repr_html_(), raw=True) 

aligned dataframes pandas styler with caption

+17
source share

Here is the Jake Vanderplas solution I came across the other day:

 import numpy as np import pandas as pd 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) 

Credit: https://github.com/jakevdp/PythonDataScienceHandbook/blob/master/notebooks/03.08-Aggregation-and-Grouping.ipynb

+10
source share

My solution just builds a table in HTML without CSS hackers and outputs it:

 import pandas as pd 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)))) list_dfs = [] list_dfs.append( pd.DataFrame(2*[{"x":"hello"}]) ) list_dfs.append( pd.DataFrame(2*[{"x":"world"}]) ) multi_column_df_display(2*list_dfs) 

Output

+9
source share

This adds the headers to @nts:

 from IPython.display import display_html def mydisplay(dfs, names=[]): html_str = '' if names: html_str += ('<tr>' + ''.join(f'<td style="text-align:center">{name}</td>' for name in names) + '</tr>') html_str += ('<tr>' + ''.join(f'<td style="vertical-align:top"> {df.to_html(index=False)}</td>' for df in dfs) + '</tr>') html_str = f'<table>{html_str}</table>' html_str = html_str.replace('table','table style="display:inline"') display_html(html_str, raw=True) 

enter image description here

+7
source share

Combining the approaches of gibbone (for setting styles and labels) and stevi (adding a space), I made my version of a function that displays pandas data frames in the form of tables side by side:

 from IPython.core.display import display, HTML def display_side_by_side(dfs:list, captions:list): """Display tables side by side to save vertical space Input: dfs: list of pandas.DataFrame captions: list of table captions """ output = "" combined = dict(zip(captions, dfs)) for caption, df in combined.items(): output += df.style.set_table_attributes("style='display:inline'").set_caption(caption)._repr_html_() output += "\xa0\xa0\xa0" display(HTML(output)) 

Using:

 display_side_by_side([df1, df2, df3], ['caption1', 'caption2', 'caption3']) 

Exit:

enter image description here

+3
source share

Gibbon answer worked for me! If you need extra space between the tables, go to the code he proposed and add this "\xa0\xa0\xa0" to the next line of code.

 display_html(df1_styler._repr_html_()+"\xa0\xa0\xa0"+df2_styler._repr_html_(), raw=True) 
+2
source share

I ended up using HBOX

 import ipywidgets as ipyw def get_html_table(target_df, title): df_style = target_df.style.set_table_attributes("style='border:2px solid;font-size:10px;margin:10px'").set_caption(title) return df_style._repr_html_() df_2_html_table = get_html_table(df_2, 'Data from Google Sheet') df_4_html_table = get_html_table(df_4, 'Data from Jira') ipyw.HBox((ipyw.HTML(df_2_html_table),ipyw.HTML(df_4_html_table))) 
+1
source share

All Articles