在iPython Notebook的HTML表格中打印2个Python Pandas DataFrame

Nyx*_*nyx 5 python ipython pandas ipython-notebook

问题:如何从1个iPython Notebook行中一起打印2个DataFrame表,这样两个表都以漂亮的表格格式显示?

以下打印只是第二个,并print df.head()不会产生漂亮的表.

df = pd.DataFrame(np.random.randn(6,4), index=pd.date_range('20150101', periods=6), columns=list('ABCD'))
df2 = pd.DataFrame(np.random.randn(6,4), index=pd.date_range('20150101', periods=6), columns=list('WXYZ'))


df.head()
df2.head()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

以下不会产生所需的漂亮表:

print df.head()
print df2.head()
Run Code Online (Sandbox Code Playgroud)

jor*_*ris 4

在 IPython 笔记本中,仅显示单元格最后一行的结果,除非明确打印或显示。

一些选项:

  • 将第二个放入df.head()下一个单元格中
  • 用于print df显式打印数据框 -> 但是,这给出了文本表示而不是 html 表示
  • 用于display(df)显式显示数据框,这提供与数据框默认显示方式相同的 html 表示形式。为此,您需要进行以下导入:

    from IPython.display import display
    
    Run Code Online (Sandbox Code Playgroud)