【发布时间】:2022-01-08 09:11:07
【问题描述】:
我可能只是不完全理解 pandas,但在使用 read_html() 并设置了 index_col 标志、修改数据框,然后尝试再次使用 to_html() 时,我遇到了一些意外行为。
这就是我的意思。我有这个 HTML 文件:
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>index</th>
<th>Avg</th>
<th>Min</th>
<th>Max</th>
</tr>
</thead>
<tbody>
<tr>
<td>build1</td>
<td>55.102323</td>
<td>37.101219</td>
<td>60.7</td>
</tr>
</tbody>
</table>
然后我使用 pandas read_html 如下:
dataFrameList = pd.read_html('empty.html', index_col=0)
df = dataFrameList[0]
这会产生如下数据框:
Avg Min Max
index
build1 55.102323 37.101219 60.7
然后我有一小段测试代码如下所示:
df.drop(['build1'], inplace=True)
df.loc['build2'] = [121212, 12443, 1290120]
print(df.to_html())
我得到以下输出:
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Avg</th>
<th>Min</th>
<th>Max</th>
</tr>
<tr>
<th>index</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>build2</th>
<td>121212.0</td>
<td>12443.0</td>
<td>1290120.0</td>
</tr>
</tbody>
</table>
我做错了什么?我试图将标志 to_html(.., index=False) 设置为关闭,但这摆脱了构建名称(我需要)。
我想要的输出(只是为了清楚)如下:
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>index</th>
<th>Avg</th>
<th>Min</th>
<th>Max</th>
</tr>
</thead>
<tbody>
<tr>
<th>build2</th>
<td>121212.0</td>
<td>12443.0</td>
<td>1290120.0</td>
</tr>
</tbody>
</table>
【问题讨论】: