有没有办法以有吸引力的方式自动呈现大熊猫数据帧

时间:2015-07-21 19:00:05

标签: python pandas dataframe data-visualization

我工作的一个重要组成部分是以有吸引力的方式呈现数据表。我在Pandas做了很多工作,通常必须导出到Excel并在那里进行演示。有谁知道在看起来很有吸引力的桌子上展示Pandas Data框架的方法有哪些?

1 个答案:

答案 0 :(得分:2)

我喜欢@ Brandon Rhodes在他出色的pandas tutorial中所采用的方法。他使用IPython笔记本,在他的笔记本开头,他添加了以下几行:

from IPython.core.display import HTML
css = open('style-table.css').read() + open('style-notebook.css').read()
HTML('<style>{}</style>'.format(css))

引用项目目录中的文件style-notebook.cssstyle-table.css。这些文件(可以在他的github page上找到)可以随意修改,但这就是他的样子。

样式notebook.css:

h3 {
    color: white;
    background-color: black;
    padding: 0.5em;
}  

样式table.css:

body {
    margin: 0;
    font-family: Helvetica;
}
table.dataframe {
    border-collapse: collapse;
    border: none;
}
table.dataframe tr {
    border: none;
}
table.dataframe td, table.dataframe th {
    margin: 0;
    border: 1px solid white;
    padding-left: 0.25em;
    padding-right: 0.25em;
}
table.dataframe th:not(:empty) {
    background-color: #fec;
    text-align: left;
    font-weight: normal;
}
table.dataframe tr:nth-child(2) th:empty {
    border-left: none;
    border-right: 1px dashed #888;
}
table.dataframe td {
    border: 2px solid #ccf;
    background-color: #f4f4ff;
}

结果表看起来非常好;例如:

pretty df table after Brandon Rhodes