在iPython Notebook中将DataFrame显示为表格 [英] Show DataFrame as table in iPython Notebook
问题描述
我正在使用iPython笔记本.当我这样做时:
I am using iPython notebook. When I do this:
df
我得到一张漂亮的桌子,上面有细胞.但是,如果我这样做:
I get a beautiful table with cells. However, if i do this:
df1
df2
它不会打印第一个漂亮的表格.如果我尝试这样做:
it doesn't print the first beautiful table. If I try this:
print df1
print df2
它以另一种格式打印出表格,该表格将列溢出并且使输出很高.
It prints out the table in a different format that spills columns over and makes the output very tall.
是否有一种方法可以强制它为两个数据集打印出漂亮的表格?
Is there a way to force it to print out the beautiful tables for both datasets?
推荐答案
您需要使用IPython显示模块中的HTML()
或display()
函数:
You'll need to use the HTML()
or display()
functions from IPython's display module:
from IPython.display import display, HTML
# Assuming that dataframes df1 and df2 are already defined:
print "Dataframe 1:"
display(df1)
print "Dataframe 2:"
display(HTML(df2.to_html()))
请注意,如果只是print df1.to_html()
,您将获得未渲染的原始HTML.
Note that if you just print df1.to_html()
you'll get the raw, unrendered HTML.
您也可以从IPython.core.display
导入,效果相同
You can also import from IPython.core.display
with the same effect
这篇关于在iPython Notebook中将DataFrame显示为表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!