I have this problem where I am able to write Pandas dataframe into excel using openpyxl and it works fine where the dataframe is written vertically.( Same column, consecutive rows)
but I want to write my dataframe horizontally ie elements in same row, consecutive columns
My dataframe is a single dimensional one like [10, 9,8,7,6]
writer = pd.ExcelWriter(fn, engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
#df.to_excel(writer, sheet_name='Sheet1', header=None, index=False)
df_2.to_excel(writer, sheet_name='Sheet3', header=None, index=False,
startcol=1,startrow=1)
writer.save()
My question is:
Can we define into this code snippet whether the data frame should be written vertically or horizontally just like startrow and startcol.
I've searched everywhere but could not find any.
Have you tried transposing the dataframe before writing it? eg
df_2.T.to_excel(writer, sheet_name='Sheet3', header=None, index=False, startcol=1,startrow=1)
This should solve your problem:
df2 = df2.transpose()
df_2.to_excel(writer, sheet_name='Sheet3', header=None, index=False,
startcol=1,startrow=1)
As mentioned here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.