use*_*540 6 html python pandas
假设我将以下数据输出为 HTML:
import pandas as pd
d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)
df.to_html(index = False, header = "true")
Run Code Online (Sandbox Code Playgroud)
我希望它也能生成页脚信息,例如</tbody>末尾的标签后面的信息。
<tfoot>\n <th>col1</th>\n <th>col2</th>\n </tr>\n </tfoot>
Run Code Online (Sandbox Code Playgroud)
这里的文档https://pandas.pydata.org/pandas-docs/version/0.23.4/ generated/pandas.DataFrame.to_html.html没有建议页脚选项,所以很好奇是否有解决方法?
许多表是动态生成的,因此硬编码是不可行的。
感谢您的任何建议
df.to_html 返回一个 str,因此您可以手动添加它。
import pandas as pd
d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)
df.to_html(index = False, header = "true")\
+"<tfoot>\n" + " ".join(["<th>"+ i +"</th>\n" for i in df.columns])+"</tr>\n </tfoot>"
Run Code Online (Sandbox Code Playgroud)
输出
'<table border="1" class="dataframe">\n <thead>\n <tr style="text-align: right;">\n <th>col1</th>\n <th>col2</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <td>1</td>\n <td>3</td>\n </tr>\n <tr>\n <td>2</td>\n <td>4</td>\n </tr>\n </tbody>\n</table><tfoot>\n<th>col1</th>\n <th>col2</th>\n</tr>\n </tfoot>'
Run Code Online (Sandbox Code Playgroud)