如何在Python中使用Pandas将数据数组写为Excel而不是按行 [英] How to write an data array to excel in a row instead of in a column using pandas in Python

查看:48
本文介绍了如何在Python中使用Pandas将数据数组写为Excel而不是按行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

....#设置excelwriter和数据框

.... #setup excelwriter and dataframe

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

输出:

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

如何获取输出为:

1    2    3    4    5

推荐答案

要在一行中输出,请使用以下命令:

To output in a line use this:

df = df.transpose()

完整代码:

#!/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

这篇关于如何在Python中使用Pandas将数据数组写为Excel而不是按行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆