gpt4 book ai didi

python - 格式化与 Pandas DataFrame 中的 to_html 相关的索引

转载 作者:行者123 更新时间:2023-11-30 22:51:29 38 4
gpt4 key购买 nike

我希望能够使用 Pandas DataFrame 中的 to_html() 直接格式化索引(列),并将其输出为 HTML?

类似于:

df = DataFrame([[1, 2]], index=['a'], columns=['A', 'B'])
print(df.to_html(formatters={
'index': lambda elem: '<a href="example.com/{}">{}</a>'.format(elem, elem)}, escape=False))

这不起作用。我没有收到链接。

我想我需要做类似的事情

dfc = df.copy()
dfc.index = ['<a href="example.com/{}">{}</a>'.format(i, i) for i in df.index]
print(dfc.to_html(escape=False))

最佳答案

您应该使用__index__而不是index:

print(df.to_html(formatters={
'__index__': lambda elem: '<a href="example.com/{}">{}</a>'.format(elem, elem)}, escape=False))

来源:The pull request that introduced that feature .

关于python - 格式化与 Pandas DataFrame 中的 to_html 相关的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38999600/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com