Pandas DataFrame 转 HTML 表格并指定列顺序
要将 DataFrame 转换为 HTML 表格并指定列的顺序,可以按照以下步骤进行操作:
- 使用 pandas 库中的 'to_html()' 函数将 DataFrame 转换为 HTML 表格。可以指定参数 'columns' 来指定要包含在 HTML 表格中的列。
html_table = df.to_html(columns=['column1', 'column2', 'column3'])
- 然后可以将生成的 HTML 表格写入一个 HTML 文件,以便进一步使用。
with open('output.html', 'w') as f:
f.write(html_table)
这样,你就可以通过打开 'output.html' 文件来查看生成的 HTML 表格,其中只包含指定的列并按照指定的顺序排列。
原文地址: https://www.cveoy.top/t/topic/qgrk 著作权归作者所有。请勿转载和采集!