在IPython Notebook的Pandas DataFrame单元格中包含HTML?

6 投票
1 回答
3295 浏览
提问于 2025-04-17 20:34

我该如何在IPython笔记本中使用DataFrame生成的表格里加入HTML内容呢?

举个例子,我想做到类似这样的事情:

pd.DataFrame({
    "text": ["Hello <strong>world</strong>"],
})

这样就能生成一个类似于下面这样的表格:

<table>
  <tr><th></th><th>text</th></tr>
  <tr><th>0</th><td>Hello <strong>world</strong></tr>
</table>

1 个回答

4

你试过用 pandas.DataFrame.to_html 吗?想了解更多信息可以查看 这里。如果你想看一些很棒的例子,关于如何在 IPython Notebook 中显示 html、svg、图片等等,可以去 这里

在 IPython Notebook 中的使用方法是这样的:

from IPython.display import HTML
data = pd.DataFrame({'A':[1,2,3,4,5],'B':[6,7,8,9,10]})
h = HTML(data.to_html());h

撰写回答