-1

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 i.e. 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.

1
  • 1
    First use df2 = df2.transpose() Commented Nov 19, 2019 at 11:40

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried transposing the dataframe before writing it ? e.g.

df_2.T.to_excel(writer, sheet_name='Sheet3', header=None, index=False, startcol=1,startrow=1)

1 Comment

I have used the same but do not get my desired result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.