编写一个 Python 程序将数据框导出到 html 文件
pythonpandasserver side programmingprogramming更新于 2024/6/21 3:24:17
假设,我们已经保存了 pandas.csv 文件并将文件导出为 Html 格式
解决方案
为了解决这个问题,我们将遵循下面给出的步骤 −
使用 read_csv 方法读取 csv 文件,如下所示 −
df = pd.read_csv('pandas.csv')
使用文件对象在写入模式下创建新文件 pandas.html,
f = open('pandas.html','w')
声明结果变量以将数据框转换为 html 文件格式,
result = df.to_html()
使用文件对象,写入结果中的所有数据。最后关闭文件对象,
f.write(result) f.close()
示例
让我们看看下面的实现以获得更好的理解 −
import pandas as pd
df = pd.read_csv('pandas.csv')
print(df)
f = open('pandas.html','w')
result = df.to_html()
f.write(result)
f.close()
输出
pandas.html <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>Id</th> <th>Data</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>1</td> <td>11</td> </tr> <tr> <th>1</th> <td>2</td> <td>22</td> </tr> <tr> <th>2</th> <td>3</td> <td>33</td> </tr> <tr> <th>3</th> <td>4</td> <td>44</td> </tr> <tr> <th>4</th> <td>5</td> <td>55</td> </tr> </tbody> </table>

相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/
打印
下一节:用 Python 编写一个程序来对给定的时间序列数据进行重新采样并找到最大月末频率 ❯❮ 上一节:编写一个 Python 程序从文件中读取 Excel 数据并读取第一列和最后一列的所有行

