How to write an data array to excel in a row instead of in a column using pandas in Python
10,856
Solution 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
Solution 2
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
Solution 3
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
Author by
user2597183
Updated on July 27, 2022Comments
-
user2597183 almost 2 years
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
-
cs95 over 6 yearsT and
transpose
are exactly the same. Mentioned here: stackoverflow.com/a/48613193/4909087 -
TigerTV.ru over 6 yearsUse transfpose() - typo
-
cs95 over 6 yearsThere was a point to my comment. Please look at the other answers before posting your own. Cheers.