Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 从dataframe生成html_Python 3.x_Pandas_Dataframe_Pandas Styles - Fatal编程技术网

Python 3.x 从dataframe生成html

Python 3.x 从dataframe生成html,python-3.x,pandas,dataframe,pandas-styles,Python 3.x,Pandas,Dataframe,Pandas Styles,对不起,我是通过熊猫处理html的新手,在生成所需格式时遇到问题 我有如下的数据帧 Category Date Avg Price - growth (%) Profit Overall profit A 4/18/2021 34.30% 706.10% 669.60% B 4/18/2021 97.40% 1994.60% 1879.

对不起,我是通过熊猫处理html的新手,在生成所需格式时遇到问题

我有如下的数据帧

Category    Date    Avg Price - growth (%)      Profit      Overall profit
A         4/18/2021      34.30%                 706.10%     669.60%
B         4/18/2021      97.40%                 1994.60%    1879.00%
C         4/18/2021      742.00%                223.60%     -482.60%
我需要为电子邮件生成如下html(我还使用df.styleing选项格式化其中一些字段和

因此,我需要进行两项更改:

  • 我需要在所有组合列中添加标题

  • 我需要将我的类别列中的值作为标题,并为每个唯一值重复所有其他列(索引的日期除外)

  • 有没有一种方法可以让我在生成html后用pandas或格式化它。 我已经通过了几个链接

    &[如何从pandas dataframe输出带有合并单元格的html表格][3]


    [3] :它们似乎相同,但有点不同

    要构建数据结构,您可以尝试以下方法

    >>> df.groupby(['Date','Category']).sum().unstack('Category').swaplevel(0,1,axis=1).sort_index(axis=1)
    
    Category          A                                 B                                  C
              AVG_Price Overall_profit   Profit AVG_Price Overall_profit    Profit AVG_Price Overall_profit   Profit
    Date
    4/18/2021    34.30%        669.60%  706.10%    97.40%       1879.00%  1994.60%   742.00%       -482.60%  223.60%
    
    
    
    
    要将其转换为html,您只需将
    添加到_html()
    ,但这不会给您提供所需的标题(总体增长报告)

    获得选举权