我有一个巨大的熊猫数据帧我转换为html表,即dataframe.to_html(),它约1000行.任何使用分页的简单方法,这样我就不必滚动整行1000行.比如说,查看前50行然后单击下一步以查看后续的50行?
我能想到的最佳解决方案包括几个外部JS库:JQuery及其DataTables插件。这将使分页工作不仅仅需要很少的努力。
让我们设置一些HTML,JS和python:
from tempfile import NamedTemporaryFile
import webbrowser
base_html = """
<!doctype html>
<html><head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.css">
<script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.js"></script>
</head><body>%s<script type="text/javascript">$(document).ready(function(){$('table').DataTable({
"pageLength": 50
});});</script>
</body></html>
"""
def df_html(df):
"""HTML table with pagination and other goodies"""
df_html = df.to_html()
return base_html % df_html
def df_window(df):
"""Open dataframe in browser window using a temporary file"""
with NamedTemporaryFile(delete=False, suffix='.html') as f:
f.write(df_html(df))
webbrowser.open(f.name)
Run Code Online (Sandbox Code Playgroud)
现在我们可以加载样本数据集进行测试:
from sklearn.datasets import load_iris
import pandas as pd
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df_window(df)
Run Code Online (Sandbox Code Playgroud)
一些注意事项:
pageLength parameter in the base_html string. This is where I defined the default number of rows per page. You can find other optional parameters in the DataTable options page.df_window function was tested in a Jupyter Notebook, but should work in plain python as well.df_window and simply write the returned value from df_html into an HTML file.| 归档时间: |
|
| 查看次数: |
2227 次 |
| 最近记录: |