2016-08-05 246 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