[問題] 如何顯示欄位名稱且並存入xlsx裡面?

看板Python作者 (賣雞排的少年)時間8年前 (2018/01/03 23:03), 8年前編輯推噓1(105)
留言6則, 1人參與, 8年前最新討論串1/1
我先附上code,本人摸索自學中,還望見諒~ import pandas as pd import html5lib import openpyxl import sys import xlwt import numpy table = pd.read_html('file:///C:/T24/Case8/db.html') T0 = table[0].dropna(axis=0) display(T0) T1 = table[1].dropna(axis=0) writer = pd.ExcelWriter('T0103.xlsx') Sh1 = T0.to_excel(writer,'Sheet1') Sh2 = T1.to_excel(writer,'Sheet2') writer.save() 在display(T0)呈現的欄位是 | 0 | 1 | 2 | 3 | 4 0 | Table24 | 43 | 14 | 13 | 1 而不是圖片綠框的欄位名稱 https://i.imgur.com/QDdfTfg.jpg
問題:請問要怎麼顯示且並存入xlsx裡面?我是漏打了甚麼嗎? 我要呈現的display(T0)欄位是 TABLE | TOTAL-NB-TP | NB-TP-EXECUTED | NB-OF-PASS | NB-OF-FAIL TABLE24 | 43 | 14 | 13 | 1 懇請指點一下<(_ _)> -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.224.40.45 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1514991813.A.B38.html ※ 編輯: han999 (36.224.40.45), 01/03/2018 23:23:19

01/04 10:27, 8年前 , 1F
從寫出的結果看來,原始的資料內應該不含你要的header
01/04 10:27, 1F

01/04 10:28, 8年前 , 2F
所以,你可以改用複寫的方式去寫進的你要的header
01/04 10:28, 2F

01/04 10:29, 8年前 , 3F
pd.DataFrame(header).transpose().to_excel(writer,
01/04 10:29, 3F

01/04 10:29, 8年前 , 4F
index=False, header=False, startrow=0)
01/04 10:29, 4F

01/04 10:31, 8年前 , 5F
上述由設定startrow來把第一列的index改成你要的header
01/04 10:31, 5F

01/04 10:33, 8年前 , 6F
然後index=False 是為了不寫入第一行的index
01/04 10:33, 6F
※ 編輯: han999 (36.224.40.45), 01/04/2018 22:11:54
文章代碼(AID): #1QJF35iu (Python)