在iPython Notebook中以HTML表格形式打印2个Python Pandas DataFrame

5

问题: 如何在一个iPython NoteBook单元格中同时打印两个DataFrame表格,以便这两个表格都呈现出pretty table格式?

以下代码只能打印第二个表格,而print df.head()无法显示pretty table。

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()

以下方式无法生成所需的漂亮表格:

enter image description here

print df.head()
print df2.head()

你是想要这里的最后一个答案吗:http://stackoverflow.com/questions/28966780/pandas-show-results-of-apply-next-to-original-dataframe 如果是,我会将其关闭为重复。 - EdChum
df.head(),df2.head() - Benjamin Rowell
1
从IPython.display导入display,然后使用display(df.head()) - joris
@joris,你应该把那个发表为答案。 - EdChum
相关链接:https://dev59.com/t18d5IYBdhLWcg3wYxc-#28507257 - EdChum
显示剩余2条评论
1个回答

4
在IPython笔记本中,除非明确打印或显示,否则只会显示单元格最后一行的结果。
一些选项:
  • Put the second df.head() in the next cell
  • Use print df to explicitly print the dataframe -> but, this gives the text representation instead of html representation
  • Use display(df) to explicitly display the dataframe, this will give the same html representation as how a dataframe is shown by default. You need the following import for this:

    from IPython.display import display
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接