将多个数据帧导出为html

时间:2018-03-29 19:25:12

标签: python dataframe

请我有一个生成多个数据帧的python脚本:df1,df2,...,df10。 理想情况下,我需要将这些数据帧全部导出到一个pdf文件中,但我意识到这非常复杂。因此,我正在尝试使用df.to_html()函数将不同的数据帧导出到一个单独的html文件中。 但是,如何将所有数据名称导出到同一个html文件中?

import numpy as np
from numpy.random import randn
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(randn(5,4),columns='W X Y Z'.split())
df1 = pd.DataFrame(randn(5,4),columns='A B C D'.split())
df.head().to_html("testhtml.html")
df1.head().to_html("testhtml.html")

使用上面的代码,第二个.to_html指令会覆盖第一个内容,导致在html文件中打印一个单独的数据帧。 有没有办法在同一个html文件中“追加”数据框? 感谢

1 个答案:

答案 0 :(得分:3)

使用.to_html()获取字符串并添加它们:

$ ipython
In [1]: import numpy as np
   ...: from numpy.random import randn
   ...: import pandas as pd
   ...: import matplotlib.pyplot as plt
   ...: 
   ...: df = pd.DataFrame(randn(5,4),columns='W X Y Z'.split())
   ...: df1 = pd.DataFrame(randn(5,4),columns='A B C D'.split())
   ...: 

In [2]: with open("a.html", 'w') as _file:
   ...:     _file.write(df.head().to_html() + "\n\n" + df1.head().to_html())
   ...:     

In [3]:                                                                                                                                     
Do you really want to exit ([y]/n)? y

现在,您将能够在同一个文件中看到这两个表:

enter image description here

$ cat a.html
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>W</th>
      <th>X</th>
      <th>Y</th>
      <th>Z</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1.565874</td>
      <td>1.612569</td>
      <td>1.213773</td>
      <td>-0.059322</td>
    </tr>
    <tr>
      <th>1</th>
      <td>-0.995417</td>
      <td>-0.279548</td>
      <td>0.204154</td>
      <td>0.803098</td>
    </tr>
    <tr>
      <th>2</th>
      <td>-0.188367</td>
      <td>-1.495322</td>
      <td>0.675200</td>
      <td>-2.432019</td>
    </tr>
    <tr>
      <th>3</th>
      <td>0.776902</td>
      <td>2.642486</td>
      <td>1.858429</td>
      <td>0.024089</td>
    </tr>
    <tr>
      <th>4</th>
      <td>1.010742</td>
      <td>0.065047</td>
      <td>1.264571</td>
      <td>-0.982195</td>
    </tr>
  </tbody>
</table>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>A</th>
      <th>B</th>
      <th>C</th>
      <th>D</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>-1.381432</td>
      <td>-0.098652</td>
      <td>-1.002030</td>
      <td>0.133971</td>
    </tr>
    <tr>
      <th>1</th>
      <td>0.284307</td>
      <td>0.566509</td>
      <td>-0.640148</td>
      <td>-0.284037</td>
    </tr>
    <tr>
      <th>2</th>
      <td>0.412460</td>
      <td>-1.326584</td>
      <td>-0.297338</td>
      <td>0.531000</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-0.456548</td>
      <td>-0.354438</td>
      <td>-0.675962</td>
      <td>0.507228</td>
    </tr>
    <tr>
      <th>4</th>
      <td>-0.393275</td>
      <td>0.462753</td>
      <td>2.198363</td>
      <td>-0.042263</td>
    </tr>
  </tbody>