我工作的一个重要组成部分是以有吸引力的方式呈现数据表。我在 Pandas 中做了很多工作,通常必须导出到 Excel 并在其中处理演示文稿。有谁知道有什么方法可以在漂亮的表格中展示 Pandas 数据框吗?
请您参考如下方法:
我喜欢@ Brandon Rhodes 的方法接受他的优秀pandas tutorial .他使用 IPython Notebook,并在笔记本的开头添加了以下行:
from IPython.core.display import HTML
css = open('style-table.css').read() + open('style-notebook.css').read()
HTML('<style>{}</style>'.format(css))
它引用了项目目录中的文件style-notebook.css 和style-table.css。这些文件(可以在他的 github page 上找到)可以随意修改,但这是他的样子。
样式笔记本.css:
h3 {
color: white;
background-color: black;
padding: 0.5em;
}
样式表.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;
}
生成的表格看起来非常漂亮;例如:
