美文网首页
2019-10-05用python pandas实现excel

2019-10-05用python pandas实现excel

作者: Felicity_S | 来源:发表于2019-10-05 16:29 被阅读0次

import pandas as pd

data1=pd.read_excel(r'F:\研究生阶段\python学习\Excel合并作业-呆鸟\1.xlsx',sheetname=0,encoding='utf-8')#data为DataFrame结构,sheetname=0表示第0张sheet,也可以使用sheet的名称sheetname=‘xxx’

data2=pd.read_excel(r'F:\研究生阶段\python学习\Excel合并作业-呆鸟\1.xlsx',sheetname=1,encoding='utf-8')

data3=pd.read_excel(r'F:\研究生阶段\python学习\Excel合并作业-呆鸟\2.xlsx',sheetname=0,encoding='utf-8')

data4=pd.read_excel(r'F:\研究生阶段\python学习\Excel合并作业-呆鸟\2.xlsx',sheetname=1,encoding='utf-8')

'''

concat()函数是一种全连接方式,它不需要对齐,直接进行合并,不需要某些列和行的索引相同,只需要把数据整合到一起

通过axis参数指定连接的轴向,该参数默认为0,按行方向进行拼接

此时的行索引为两个表原来各自的索引

若想重置行索引,可以在concat()函数中设置ignore_index=True,忽略原有索引

'''

result=pd.concat([data1,data2,data3,data4],ignore_index=True)

'''

要将result中的'产品','数量'列数据写入Excel工作簿并忽略行索引信息

'''

result.to_excel(r'C:\Users\songz\Desktop\11.xlsx',columns=['产品','数量'],encoding='utf-8',index=False)

相关文章

网友评论

      本文标题:2019-10-05用python pandas实现excel

      本文链接:https://www.haomeiwen.com/subject/mvxypctx.html