2016-08-05 245 views
26

我有两个熊猫数据框,我想在Jupyter笔记本中显示它们。Jupyter笔记本并排显示两个熊猫表

做这样的事情:

display(df1) 
display(df2) 

显示他们一个低于另:

enter image description here

我想对第一个右侧的第二个数据帧。有a similar question,但它看起来像一个人满意或者合并在一个数据框中显示它们之间的差异。

这不适合我。在我的情况下,数据框可以表示完全不同的(不可比的元素),它们的大小可能不同。因此我的主要目标是节省空间。

+0

我张贴杰克Vanderplas'解决方案。干净的代码。 – Private

回答

28

您可以覆盖输出代码的CSS。它默认使用flex-direction: column。请尝试将其更改为row。这里有一个例子:

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

你可以,当然,自定义CSS,你想进一步。

如果您只想定位一个单元的输出,请尝试使用:nth-child()选择器。例如,该代码将修改仅第5单元的笔记本输出的CSS:

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

如果我想给他们两个单独的标题怎么办?试图做到这一点,无法做到这一点 –

+2

这个解决方案影响所有的细胞,我如何才能做到这一点只有一个细胞? – jrovegno

+0

@NeerajKomuravalli这可能是最好的问这是一个新的问题。我不确定一个简单的方法来做到这一点从我的头顶。 – zarak

6

我的解决办法只是建立在HTML表中没有任何CSS黑客和输出:

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

25

我已经结束了写一个函数可以做到这一点:

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) 

用法示例:

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

+0

这真的很棒,谢谢。你认为在每个输出上面添加数据框名称有多容易或者不然? –

+1

会出现两个问题:1.知道数据框的名称超出范围imho https://stackoverflow.com/questions/2749796/how-to-get-the-original-variable-name-of-variable-传递到一个函数,但可以做https://stackoverflow.com/questions/218616/getting-method-parameter-names-in-python,或将它们作为参数传递)2.您需要额外的html,并打开它结束/取决于你该怎么做...这里是这部分内容的基本示例:https://i.stack.imgur.com/mIVsD.png – ntg

6

这里是杰克Vanderplas'我整个就在几天前就解决方案:

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) 

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