Python中 pandas 数据框的超链接 [英] Hyperlinks in a Pandas Dataframe in Python

查看:221
本文介绍了Python中 pandas 数据框的超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下几行代码:

  class summary_tables():
def create_table(self,summary ):
formatting = formatter()
table = pybloqs.HTMLJinjaTableBlock(summary,formatters = formatting.summary_format(),use_default_formatters = False)
final_summary = pybloqs.VStack([table])
return final_summary

class formatter():
def summary_format(self):
table_center = pybloqs.FmtAlignTable(align ='center')
align_cells = pybloqs.FmtAlignCellContents(alignment ='center',apply_to_header_and_index = False)
header = pybloqs.FmtHeader(fixed_width ='auto',column_width ='5cm',index_width ='1cm',top_padding ='2cm')
heatmap1 = pybloqs.FmtHeatmap(axis = 1,max_color = colors.HEATMAP_RED,min_color = colors.HEATMAP_RED,threshold = 0.01)
return [table_center,align_cells,header,heatmap1]

这是帮助我从一个名为 summary_df 的<熊猫数据框构建和格式化一个html文件:

  mkt price_diff 
0 APPL'< a href =http://~price/APPL_breakdown.html> 3< / a> '
1 GOOG'< a href =http://~price/GOOG_breakdown.html> 0< / a>'
2 MSFT'< a href =http:// 〜price / MSFT_breakdown.html> 2< / a>'

/ b>

  tables = summary_tables()
summary_block = tables.create_table(summary_df)
file = os.path。加入(os.path.dirname('/ home / jbloggs /'),'Summary.html')
block_final = pybloqs.VStack([summary_block])
block_final.save(文件)

当我打开 html 时,文件除了突出显示外。在我的类格式化程序()中查看 def summary_format()试图建立啊eatmap:

  heatmap1 = pybloqs.FmtHeatmap(axis = 1,max_color = colors.HEATMAP_RED,min_color = colors.HEATMAP_RED,threshold = 0.01)

问题是.. my DatFrame 将该号码附加到超链接:

 '< a href =http://~price/APPL_breakdown.html> ; 3< / a>'

在这里您可以看到指向 APPL_breakdown.html 文件链接到数字 3 ..



我可以解决这个问题,以便格式化程序函数知道查看数字而不是超链接?

解决方案

通过将任务分成两步,我曾经取得了非常类似的结果:

<1>在原始数据帧中,只有数值。然后,heatmap格式化程序应该很容易地拿起它。



2)然后将单元格内容更改为单独的格式化程序中的字符串,该格式程序将单元格值(数字)超链接。这与将float转换为漂亮的打印格式类似,因此您可以使用例如 FmtDecimals 作为模板(参见这个)。由于您的转换为超链接格式化程序对于您的应用程序非常具体,因此您可以将该格式化程序代码保留在本地模块中,并且不需要修改pybloq本身。只需将格式化程序添加到格式化程序列表的末尾即可。这是必要的,因为它会修改单元格内容,再次类似漂亮打印浮动的格式。


I have the following bit of code:

class summary_tables():
    def create_table(self, summary):
        formatting = formatter()
        table = pybloqs.HTMLJinjaTableBlock(summary,formatters=formatting.summary_format(), use_default_formatters=False)
        final_summary = pybloqs.VStack([table])
        return final_summary

class formatter():
    def summary_format(self):
        table_center = pybloqs.FmtAlignTable(alignment='center')
        align_cells = pybloqs.FmtAlignCellContents(alignment='center', apply_to_header_and_index=False)
        header = pybloqs.FmtHeader(fixed_width='auto', column_width='5cm', index_width='1cm', top_padding='2cm')
        heatmap1 = pybloqs.FmtHeatmap(axis=1, max_color=colors.HEATMAP_RED, min_color=colors.HEATMAP_RED, threshold=0.01)
        return [table_center, align_cells, header, heatmap1]

This is to help me build and format a html file from a Pandas dataframe called summary_df:

        mkt    price_diff
    0   APPL   '<a href="http://~price/APPL_breakdown.html">3</a>' 
    1   GOOG   '<a href="http://~price/GOOG_breakdown.html">0</a>'
    2   MSFT   '<a href="http://~price/MSFT_breakdown.html">2</a>'

So when I run:

tables = summary_tables()
summary_block = tables.create_table(summary_df)
file = os.path.join(os.path.dirname('/home/jbloggs/'), 'Summary.html')
block_final = pybloqs.VStack([summary_block])
block_final.save(file)

I see everything I expect when I open up the html file apart from the highlighting.. Looking at def summary_format() in my class formatter() you can see I'm trying to build a heatmap:

heatmap1 = pybloqs.FmtHeatmap(axis=1, max_color=colors.HEATMAP_RED, min_color=colors.HEATMAP_RED, threshold=0.01)

Problem is.. my DatFrame attaches the number to the hyperlink:

'<a href="http://~price/APPL_breakdown.html">3</a>'`

Here you can see that the link to the APPL_breakdown.html file is linked to the number 3..

How can I resolve this so that the formatter function knows to look at the number rather than the hyperlink??

解决方案

I once achieved something very similar by splitting the task into two steps:

1) In the raw dataframe, have only the numerical value. Then the heatmap formatter should pick it up easily.

2) Then change the cell contents to a string in a separate formatter that combines the cell value (the number) with the hyperlink. This is similar to converting a float to a pretty-printed format, so you could use e.g. FmtDecimals as a template (see this).

Since your convert-to-hyperlink formatter is quite specific to your application, you can keep that formatter code local in your module and you do not need to modify pybloqs itself. Just add the formatter to the end of the list of formatters. This is necessary since it will modify the cell contents, again similar to the formatters for pretty-printing floats.

这篇关于Python中 pandas 数据框的超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆