IPython Notebook只会显示一个单元格中的最后一个返回值。对于您的情况,最简单的解决方案是使用两个单元格。
如果您真的只需要一个单元格,可以像这样使用hack:
class A:
def _repr_html_(self):
return salaries.head()._repr_html_() + '</br>' + teams.head()._repr_html_()
A()
如果您经常需要此操作,请将其制作成一个函数:
def show_two_heads(df1, df2, n=5):
class A:
def _repr_html_(self):
return df1.head(n)._repr_html_() + '</br>' + df2.head(n)._repr_html_()
return A()
使用方法:
show_two_heads(salaries, teams)
超过两个人的版本:
def show_many_heads(*dfs, n=5):
class A:
def _repr_html_(self):
return '</br>'.join(df.head(n)._repr_html_() for df in dfs)
return A()
使用方法:
show_many_heads(salaries, teams, df1, df2)
from IPython.core.interactiveshell import InteractiveShell' InteractiveShell.ast_node_interactivity = "all"
从IPython.core.interactiveshell导入InteractiveShell' InteractiveShell.ast_node_interactivity = "all"
- user7437554