pandas xlsxwriter, not able to shift columns/row using xlsxwriter

Viewed 233

I have been trying to shift rows and columns while exporting data to excel sheet but not able to do that. I wrote the following code-

import pandas as pd
writer = pd.ExcelWriter(filename, engine='xlsxwriter')
workbook = writer.book
df.to_excel(writer, index=False, sheet_name='mydata')
worksheet = writer.sheets['Index Levels']
writer.save()

Any idea how to to do it?

2 Answers

I think you can explicitly mention row and column like shown below

df.to_excel(writer, index=False, sheet_name='mydata', startcol=value,
startrow=value)

please refer to https://xlsxwriter.readthedocs.io/working_with_pandas.html and you will find solution as mentioned below

# Position the dataframes in the worksheet.
df1.to_excel(writer, sheet_name='Sheet1')  # Default position, cell A1.
df2.to_excel(writer, sheet_name='Sheet1', startcol=3)
df3.to_excel(writer, sheet_name='Sheet1', startrow=6)

have you tried to transpose the dataframe before writing it to a sheet?

df1_transposed = df1.T

This will switch the rows and columns

If this is not what you are after please do further explain what is "shifting"

Related