在 pandas 上分页dataframe.to_html() [英] Pagination on pandas dataframe.to_html()
问题描述
我有一个巨大的熊猫数据框,我正在将其转换为html表,即dataframe.to_html()
,它大约有1000行.使用分页的任何简便方法都无需滚动整个1000行.例如,查看前50行,然后单击下一步"查看随后的50行?
I have a huge pandas dataframe I am converting to html table i.e. dataframe.to_html()
, its about 1000 rows. Any easy way to use pagination so that I dont have to scroll the whole 1000 rows. Say, view the first 50 rows then click next to see subsequent 50 rows?
推荐答案
我能想到的最佳解决方案包括几个外部JS库: DataTables插件.这将使分页所需要的不仅仅需要很少的努力.
The best solution I can think of involves a couple of external JS libraries: JQuery and its DataTables plugin. This will allow for much more than pagination, with very little effort.
让我们设置一些HTML,JS和python:
Let's set up some HTML, JS and 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)
现在我们可以加载示例数据集进行测试:
And now we can load a sample dataset to test it:
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)
美丽的结果:
一些注意事项:
- 注意
base_html
字符串中的pageLength
参数.这是我定义每页默认行数的地方.您可以在DataTable 选项页面中找到其他可选参数. -
df_window
函数已在Jupyter Notebook中进行了测试,但也应在纯python中工作. - 您可以跳过
df_window
并简单地将df_html
中返回的值写入HTML文件中.
- Notice the
pageLength
parameter in thebase_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. - The
df_window
function was tested in a Jupyter Notebook, but should work in plain python as well. - You can skip
df_window
and simply write the returned value fromdf_html
into an HTML file.
这篇关于在 pandas 上分页dataframe.to_html()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!