python - 如何使用 pandas 数据框将子标题添加到 html 表中以及我们如何将数据框数据访问到 html 表中?

标签 python pandas

下面是我的数据框

html = data.to_html()

我得到的输出表是:

enter image description here

我希望输出表为:

enter image description here

代码:

data = [[0,65247.0,53306.0,112.0,6524.0,53306.0,112.0], [1,55695.0,43214.0,0.0,5569.0,43214.0,0.0], [2,78345.0,65189.0,0.0,7834.0,65189.0,0.0], [3,16151.0,14057.0,0.0,1615.0,14057.0,0.0],[4,13960.0,12062.0,0.0,1396.0,12062.0,0.0]] 

df = pd.DataFrame(data, columns = ['HOUR','SUB','DEL %','WAIT %', 'SUB','DEL %','WAIT %']) 

最佳答案

有重复的列不是一个好主意,添加后缀会有所帮助

df.columns = pd.MultiIndex.from_tuples((('', 'HOUR'), ('BSNL_WEST', 'SUB'), ('BSNL_WEST', 'DEL %'), ('BSNL_WEST ', 'WAIT %'), ('BSNL_NORTH', 'SUB_2'), ('BSNL_NORTH', 'DEL %_2'), ('BSNL_NORTH', 'WAIT %_2')))

结果

         BSNL_WEST                 BSNL_NORTH
  HOUR       SUB    DEL % WAIT %      SUB_2  DEL %_2 WAIT %_2
0    0   65247.0  53306.0  112.0     6524.0  53306.0    112.0
1    1   55695.0  43214.0    0.0     5569.0  43214.0      0.0
2    2   78345.0  65189.0    0.0     7834.0  65189.0      0.0
3    3   16151.0  14057.0    0.0     1615.0  14057.0      0.0
4    4   13960.0  12062.0    0.0     1396.0  12062.0      0.0

关于python - 如何使用 pandas 数据框将子标题添加到 html 表中以及我们如何将数据框数据访问到 html 表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59451243/

相关文章:

python命令替换linux命令

python - AWS ECS 环境变量不可用 [Python]

python - 通过压缩将 2 个 numpy 维度 reshape 为一个维度?

python - 用 fillna 替换空值仍然会在 Pandas 中返回空值

python - Pandas 数据帧的条件附加列表

python - Pandas 在数据框中的字符串旁边获取值

python - 如果使用 pandas 一列超过 10 个字符,我如何删除整行

python - 在 Pandas 数据框中写入行并将其附加到现有数据框中

python - 正则表达式提取可选组内的组

python - socket.error[Errno 99] 无法分配请求的地址