Python Pandas: How to specify the starting cell position when exporting dataframe to Excel

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-19 06:16:52

问题


When a dataframe is exported from Pandas to Excel using xlsxwriter, it seems to put the table at cell A1 by default.

Is there a way to change this? I don't mind inserting rows and columns to move the table away from A1, as long as it's done programmatically via pandas or xlsxwriter.

In case it helps, here is my code.

writer = pd.ExcelWriter(r'c:\file.xlsx', engine = 'xlsxwriter')
workbook - writer.book
df.to_excel(writer, index=True, sheet_name ='Sheet1')

I couldn't find any XlsxWriter method for inserting rows and columns either.


回答1:


to_excel accepts startrow and startcol arguments which are zero-based numbers (ie startrow=1 and startcol=1 will put the top left cell of the table in cell B2).

startrow : upper left cell row to dump data frame

startcol : upper left cell column to dump data frame



来源:https://stackoverflow.com/questions/36957157/python-pandas-how-to-specify-the-starting-cell-position-when-exporting-datafram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!