Python+Pandas批量合并大量excel文件
requirments.txt
et-xmlfile==1.1.0
numpy==1.24.3
openpyxl==3.1.2
pandas==2.0.1
python-dateutil==2.8.2
pytz==2023.3
six==1.16.0
tzdata==2023.3
main.py
import os
import pandas as pd
dir_path = os.path.dirname(os.path.abspath(__file__))
source_location = os.path.join(dir_path,"source")
df = []
target = []
i = 0
for filename in os.listdir(source_location):
if filename.endswith('.xlsx'):
df.append(filename)
for i in range (0,len(df)):
# for i in range (0,2):
excel_file_location = os.path.join(source_location,df[i])
print(excel_file_location)
dfs = pd.read_excel(excel_file_location)
dfs.insert(loc=18,column='file_name',value=df[i])
target.append(dfs)
i = i +1
df_concat = pd.concat(target)
df_concat.to_excel(dir_path+'/{}.xlsx'.format('合并结果'),index=None)
df_concat.to_csv('df_concat.csv',sep=',',header=True, encoding='utf_8_sig', index=False)
print(i)
excel文件放在source目录下
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
2021-05-05 python通过递归清除字典中value为特定值的key