2
a= [1,2,3,4,5]
df=DataFrame(a)

.... #setup excelwriter and dataframe

df.to_excel(writer, sheet_name=sheetname,startrow=1, startcol=1, header=False, index=False)

Output:

1\n
2\n
3\n
4\n
5

How can I get output as:

1    2    3    4    5
0

3 Answers 3

3

You could transpose first, and then save to excel:

df

   0
0  1
1  2
2  3
3  4
4  5

df.T.to_excel(writer, 
              sheet_name=sheetname,
              startrow=1, 
              startcol=1, 
              header=False, 
              index=False)

1   2   3   4   5
Sign up to request clarification or add additional context in comments.

Comments

1

To output in a line use this:

df = df.transpose()

Full code:

#!/usr/bin/python
import pandas as pd 
import xlsxwriter as xlsw

a = [1,2,3,4,5]
df = pd.DataFrame(a)
df = df.transpose()
xlsfile = 'pandas_simple.xlsx'
writer = pd.ExcelWriter(xlsfile, engine='xlsxwriter')

df.to_excel(writer, sheet_name="Sheet1Name",startrow=1, startcol=1, header=False, index=False)

https://github.com/tigertv/stackoverflow-answers

Comments

1

Use transpose() on dataframe to convert columns into rows.

import pandas as pd

a= [1,2,3,4,5]
df=pd.DataFrame(a)
df = df.transpose()

print(df)

result:

   0  1  2  3  4
0  1  2  3  4  5

3 Comments

T and transpose are exactly the same. Mentioned here: stackoverflow.com/a/48613193/4909087
Use transfpose() - typo
There was a point to my comment. Please look at the other answers before posting your own. Cheers.

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.