pandas 在 iPython Notebook 中将 DataFrame 显示为表格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26873127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:37:16  来源:igfitidea点击:

Show DataFrame as table in iPython Notebook

pandasprintingipython-notebookjupyter-notebookdisplay

提问by Chris

I am using iPython notebook. When I do this:

我正在使用 iPython 笔记本。当我这样做时:

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?

有没有办法强制它打印出两个数据集的漂亮表格?

回答by emunsing

You'll need to use the HTML()or display()functions from IPython's display module:

您需要使用IPython 显示模块中的HTML()display()函数:

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

Note that if you just print df1.to_html()you'll get the raw, unrendered HTML.

请注意,如果您只是print df1.to_html()获得原始的、未渲染的 HTML。

You can also import from IPython.core.displaywith the same effect

您也可以从IPython.core.display相同的效果导入

回答by JacobWuzHere

from IPython.display import display
display(df)  # OR
print df.to_html()

回答by Jonny Brooks

This answer is based on the 2nd tip from this blog post: 28 Jupyter Notebook tips, tricks and shortcuts

此答案基于此博客文章中的第二个提示:28 Jupyter Notebook 提示、技巧和快捷方式

You can add the following code to the top of your notebook

您可以将以下代码添加到笔记本顶部

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

This tells Jupyter to print the results for any variable or statement on it's own line. So you can then execute a cell solely containing

这告诉 Jupyter 在它自己的行上打印任何变量或语句的结果。因此,您可以执行一个仅包含

df1
df2

and it will "print out the beautiful tables for both datasets".

它将“打印出两个数据集的漂亮表格”。

回答by Shital Shah

I prefer not messing with HTML and use as much as native infrastructure as possible. You can use Output widget with Hbox or VBox:

我不喜欢弄乱 HTML 并尽可能多地使用本机基础设施。您可以将输出小部件与 Hbox 或 VBox 一起使用:

import ipywidgets as widgets
from IPython import display
import pandas as pd
import numpy as np

# sample data
df1 = pd.DataFrame(np.random.randn(8, 3))
df2 = pd.DataFrame(np.random.randn(8, 3))

# create output widgets
widget1 = widgets.Output()
widget2 = widgets.Output()

# render in output widgets
with widget1:
    display.display(df1)
with widget2:
    display.display(df2)

# create HBox
hbox = widgets.HBox([widget1, widget2])

# render hbox
hbox

This outputs:

这输出:

enter image description here

在此处输入图片说明

回答by Moondra

It seems you can just display both dfs using a comma in between in display. I noticed this on some notebooks on github. This code is from Jake VanderPlas's notebook.

似乎您可以在显示中使用逗号来显示两个 dfs。我在 github 上的一些笔记本上注意到了这一点。此代码来自 Jake VanderPlas 的笔记本。

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)

display('df', "df2")

display('df', "df2")

回答by Hossein SLT

In order to show the DataFrame in Jupyter Notebook just type:

为了在 Jupyter Notebook 中显示 DataFrame,只需键入:

   display(Name_of_the_DataFrame)

for example:

例如:

  display(df)

回答by BSalita

To display dataframes contained in a list:

要显示列表中包含的数据框:

display(*dfs)