excle的几种写出方式

发布时间 2023-07-08 23:50:40作者: sbsdnyn
import pandas

lis1 = ['a1', 'b1', 'c1', 'd1', 'e1']
lis2 = ['a2', 'b2', 'c2', 'd2', 'e2']
lis3 = ['a3', 'b3', 'c3', 'd3', 'e3']

# 1、字典样子,list代表一列,字典的key是表头行,value是列内容。
data = {'header1': lis1, 'header2': lis2, 'header3': lis3}
df = pandas.DataFrame(data)
df.to_excel('1表为列.xlsx')
df.to_excel('1表为列_.xlsx', index=False, header=False)

# 2、列表样子,list代表一行。表头要在pandas.DataFrame指明,不指明默认用序号。当然可以用header=False把它去掉。
data = [lis1, lis2, lis3]
df = pandas.DataFrame(data, columns=['header1', 'header2', 'header3', 'header4', 'header5'])
df.to_excel('2表为行.xlsx')
df = pandas.DataFrame(data)  # 不要表头就不用去写。
df.to_excel('2表为行_.xlsx', index=False, header=False)

print('写出完毕。')

# 还有一种写出方法,不知道有什么区别,感觉上面的一句话更方便。
# writer = pandas.ExcelWriter('xxx.xlsx')
# df.to_excel(writer)
# writer.close()