如何使用python合并多个excel文件
适用场景
当你有多个列名一致的excel文件的时候,你想要把多个excel文件合并成一个excel文件
Python代码实现
- 首先导入需要的库
import pandas as pd
import os
- 将所有需要合并的excel放进一个单独的文件夹里
- 定义一个函数
def append(path): #path:所有需要合并的excel文件所在的文件夹filename_excel = [] # 建立一个空list,用于储存所有需要合并的excel名称frames = [] # 建立一个空list,用于储存dataframefor root, dirs, files in os.walk(path):for file in files:file_with_path = os.path.join(root, file) filename_excel.append(file_with_path)df = pd.read_excel(file_with_path, engine='openpyxl')frames.append(df)df = pd.concat(frames, axis=0)return df
一些说明
- 上面的代码中root是就是当前文件夹的所有路径
- files是一个list, 包含文件夹中所有excel的名称
- os.path.join(root, file)就是合并文件夹的路径和文件名称,这样后面的pd.read_excel()就能读取excel文件
tips
也可以不定义函数直接用:
filename_excel = []
frames = []
for root, dirs, files in os.walk(path):for file in files:file_with_path = os.path.join(root, file) filename_excel.append(file_with_path)df = pd.read_excel(file_with_path, engine='openpyxl')frames.append(df)
df = pd.concat(frames, axis=0)
df.to_excel("合并的excel.xlsx")
特殊情况
如果excel的文件名包括日期,且需要写到最后汇总的excel中
def append(path): filename_excel = []frames = [] for root, dirs, files in os.walk(path):for file in files:file_with_path = os.path.join(root, file) filename_excel.append(file_with_path)df = pd.read_excel(file_with_path, engine='openpyxl')# 将文件名中包含的日期信息写入dataframedf["日期"] = pd.to_datetime(file.strip('.xls')[-1:])#日期在什么位置需要自己调整frames.append(df)df = pd.concat(frames, axis=0)return df
如果将多个excel合并到一个excel中,sheet命名为excel的名字
def combine(path):with pd.ExcelWriter("合并的excel.xlsx") as writer:for root, dirs, files in os.walk(path):for file in files:filename = os.path.join(root, file)df = pd.read_excel(filename, engine='openpyxl')df.to_excel(writer, sheet_name=file.strip('.xls')) #删除文件名的后缀,有时候是.csv/.xlsxreturn df
如何使用python合并多个excel文件相关推荐
- python合并两个excel文件_Python将多个excel文件合并为一个文件
利用Python,将多个excel文件合并为一个文件 思路 利用python xlrd包读取excle文件,然后将文件内容存入一个列表中,再利用xlsxwriter将内容写入到一个新的excel文件中 ...
- python合并两个excel文件_利用Python将多个excel文件合并为一个文件
# -*- coding: utf-8 -*- #导入需要使用的包 import xlrd #读取Excel文件的包 import xlsxwriter #将文件写入Excel的包 #打开一个e ...
- python合并excel文件关键字_python合并多个excel文件的示例
工作中经常遇到要将十几个Excel(不管是xls.或者是CSV)合并到同一个文件中去,手工一个一个复制是不可能的,此时就轮到Python出马了 主要是利用for循环,读取每一个文件,作为df,然后再通 ...
- python合并多个excel的某些字段_python合并多个excel文件的示例
工作中经常遇到要将十几个Excel(不管是xls.或者是CSV)合并到同一个文件中去,手工一个一个复制是不可能的,此时就轮到Python出马了 主要是利用for循环,读取每一个文件,作为df,然后再通 ...
- Python读取多个excel文件(删除字段、数据格式转换、dataframe多表合并)并写入ElasticSearch实战(自动创建索引、写入ElasticSearch、探索性数据分析)
Python读取多个excel文件(删除字段.数据格式转换.dataframe多表合并)并写入ElasticSearch实战(自动创建索引.写入ElasticSearch.探索性数据分析) 目录
- python处理多个excel文件-Python将多个excel文件合并为一个文件
利用Python,将多个excel文件合并为一个文件 思路 利用python xlrd包读取excle文件,然后将文件内容存入一个列表中,再利用xlsxwriter将内容写入到一个新的excel文件中 ...
- python合并多个csv文件为excel并绘制曲线(参考文章记录)
1 Python将多个excel文件合并为一个文件 https://blog.csdn.net/jiahaowanhao/article/details/80547623?utm_medium=dis ...
- python学习之路:合并多个excel文件、合并多个pdf文件
**关键点: python中的os模块 python中的pandas中的concat python中的PyPDF2模块** 1.合并多个Excel文件 import os import pandas ...
- 合并数据 - 合并多个Excel文件并转成CSV - Python代码
在工作中,有时候因为部门间的数据权限问题,推送数据的时候往往是通过邮件完成,对于量大的数据,往往会拆成很多个excel发送,到下一个部门导入数据库之前,总是需要先将所有excel合成一个excel,再 ...
- excel调用python编程-使用Python和xlwt向Excel文件中写入中文的实例
sheet1.write(row_num,0,f) row_num = 1 book.save('demo.xls') 程序执行结果如下: grey@DESKTOP-3T80NPQ:/mnt/e/01 ...
最新文章
- shiro和Spring整合使用注解时没有执行realm的doGetAuthorizationInfo回调方法的解决
- iOS架构-静态库.a的打包及使用(2)
- 读取list java_java 分批次读取java.util.List 数据
- 使用maven生成scala项目的目录结构
- Python提取.ipynb文件中的Python代码保存为.py文件
- 编程语言_JavaScript_面试题004
- Sql 列转行 三种方法对比
- MDR的进阶版本-GMDR
- 【网络安全】SQL注入详细分析
- 判断斗地主卡牌类型的思路
- NBA球队也使用Salesforce?
- GAMES101-现代计算机图形学入门-闫令琪 - lecture11 几何3(Geometry 3) - 课后笔记
- 计算机如何算幂函数,幂函数(乘方)|指数(函数)|对数(函数)|及其运算法则...
- 在服务端录制语音视频
- 【Beta】Scrum Meeting 7 与助教谈话
- 使用AW9523B芯片驱动16路LED时,LED出现误点亮的问题
- 从苏宁电器到卡巴斯基第20篇:曲折考研路(补)
- 520 html模板,520表白酷炫html
- centos7 更新网络源,下载扩展源时出现“one of the configured repositories failed”提示,解决方案
- 跨站脚本执行漏洞详解
热门文章
- cad批量选择相同块_在CAD中如何快速选择相同或类似的图形、图块?
- java 遍历json串_Java遍历Json数据
- 【高手分享】熟知Win7系统(英文版)分区14个小步骤
- 用slmgr命令激活正版Win7旗舰版系统
- python自带的pip版本过低 卸载pip重写安装V10
- 嵌入式软件设计之设计模式
- 第三方登录微信登录流程
- 词法分析之LED文件生成程序【调试中......】
- matlab 并联机器人,MATLAB在并联机器人运动仿真中的应用
- solidworks验证许可闪退_彻底解决SolidWorks“无法获得下列许可 SOLIDWORKS Standard”的激活错误...