這是一張桌子
df = {'index':['Larry','Moe','Curly'],
'age':[54,58,65],
'eye':['blue','brown','brown'],
'fortune':[1,1.5,1.2],
'food':['pizza','pasta','burgers'],
'job':['actor','actor','actor'],
'kids':[2,3,4]}
df = pd.DataFrame(df)
df = df.set_index('index')
我想從這張表中創建 3 個迷你表,并將每個迷你表保存為 1 個 excel 檔案中的作業表。第一階段應包含列年齡和眼睛,第二階段,財富和食物,第三階段,作業和孩子。
這是我嘗試過的,但沒有多大意義
col = df.columns
with pd.Excelwriter('filename' '.xlsx') as xlswriter:
for col in df:
col = col[::2]
df.to_excel(xlswriter, index=False, sheet_name = 'sheet' )
uj5u.com熱心網友回復:
如果您希望將每兩列保存到單獨的作業表中,請嘗試:
col = df.columns
# ExcelWriter not Excelwriter
with pd.ExcelWriter('filename' '.xlsx') as xlswriter:
for i in range(0,len(col), 2):
# you can use `iloc` to slice by column number
df.iloc[:,i:i 2].to_excel(xlswriter, index=False, # are you sure you want to remove the index?
sheet_name = f'sheet {i}' ) # change sheet name so you don't override your data
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/384595.html
