我正在Excel中编写一些列表的值。除了标题之外,每列都会获取所有值。对于每一列,我的标题名为“0”。
包含Excel中写入的部分代码:
writer=pd.ExcelWriter('/media/sf_vboxshared/workbook.xlsx', engine='xlsxwriter')
df0=pd.DataFrame(A)
df1=pd.DataFrame(B)
df2=pd.DataFrame(C)
df3=pd.DataFrame(D)
df4=pd.DataFrame(E)
df5=pd.DataFrame(F)
df6=pd.DataFrame(G)
df7=pd.DataFrame(H)
df8=pd.DataFrame(I)
df0.to_excel(writer, sheet_name="Sheet1", startcol=0, startrow=1,header="A", index=False)
df1.to_excel(writer, sheet_name="Sheet1", startcol=1, startrow=1,header="B", index=False)
df2.to_excel(writer, sheet_name="Sheet1", startcol=2, startrow=1,header="C", index=False)
df3.to_excel(writer, sheet_name="Sheet1", startcol=3, startrow=1,header="D", index=False)
df4.to_excel(writer, sheet_name="Sheet1", startcol=4, startrow=1,header="E", index=False)
df5.to_excel(writer, sheet_name="Sheet1", startcol=5, startrow=1,header="F", index=False)
df6.to_excel(writer, sheet_name="Sheet1", startcol=6, startrow=1,header="G", index=False)
df7.to_excel(writer, sheet_name="Sheet1", startcol=7, startrow=1,header="H", index=False)
df8.to_excel(writer, sheet_name="Sheet1", startcol=8, startrow=1,header="I", index=False)
writer.save()
writer.close()
如何用每列的名称修复它?
最好的问候,丹
在你的columns=[header]
构造函数中使用pd.Dataframe
,即
df0=pd.DataFrame(A,columns=['A'])
df1=pd.DataFrame(B,columns=['B'])
.
.
.
为什么不使用单一的DataFrame
?
writer=pd.ExcelWriter('/media/sf_vboxshared/workbook.xlsx', engine='xlsxwriter')
df = pd.DataFrame({'A': A, 'B': B,'C': C, 'D': D, 'E': E, 'F': F, 'G':G, 'H': H, 'I': I})
df = df.to_excel(writer, 'Sheet1', index=False)