Python怎么实现Excel文件的合并
发表于:2025-11-14 作者:千家信息网编辑
千家信息网最后更新 2025年11月14日,本文小编为大家详细介绍"Python怎么实现Excel文件的合并",内容详细,步骤清晰,细节处理妥当,希望这篇"Python怎么实现Excel文件的合并"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢
千家信息网最后更新 2025年11月14日Python怎么实现Excel文件的合并
本文小编为大家详细介绍"Python怎么实现Excel文件的合并",内容详细,步骤清晰,细节处理妥当,希望这篇"Python怎么实现Excel文件的合并"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
一、单目录下面的数据合并

将2020下的所有文件进行合并,成一个文件:
import requestsimport jsonimport openpyxlimport datetimeimport datetime as dtimport timeimport pandas as pdimport csvfrom openpyxl import load_workbookfrom sqlalchemy import create_engineimport mathimport osimport glob
csv_list=glob.glob(r'D:\Python\03DataAcquisition\COVID-19\2020\*.csv')print("所有数据文件总共有%s" %len(csv_list))for i in csv_list: fr=open(i,"rb").read() #除了第一个数据文件外,其他不读取表头 with open('../output/covid19temp0314.csv','ab') as f: f.write(fr) f.close()print('数据合成完毕!')
合并后的数据:
二、使用函数进行数据合并
## 02 使用函数进行数据合并import osimport pandas as pd # 定义函数(具有递归功能)def mergeFile(parent,path="",pathdeep=0,filelist=[],csvdatadf=pd.DataFrame(),csvdata=pd.DataFrame()): fileAbsPath=os.path.join(parent,path) if os.path.isdir(fileAbsPath)==True: if(pathdeep!=0 and ('.ipynb_checkpoints' not in str(fileAbsPath))): # =0代表没有下一层目录 print('--'+path) for filename2 in os.listdir(fileAbsPath): mergeFile(fileAbsPath,filename2,pathdeep=pathdeep+1) else: if(pathdeep==2 and path.endswith(".csv") and os.path.getsize(parent+'/'+path)>0): filelist.append(parent+'/'+path) return filelist# D:\Python\03DataAcquisition\COVID-19path=input("请输入数据文件所在目录:")filelist=mergeFile(path)filelistcsvdata=pd.DataFrame()csvdatadf=pd.DataFrame()for m in filelist: csvdata=pd.read_csv(m,encoding='utf-8-sig') csvdatadf=csvdatadf.append(csvdata)# 由于2023年的数据还没有,所以不合并(* ̄(oo) ̄)注: 这个的等待时间应该会比较长,因为一共有一百九十多万条数据。
将合并后的数据进行保存:
csvdatadf.to_csv("covid190314.csv",index=None,encoding='utf-8-sig')csvdatadf=pd.read_csv("covid190314.csv",encoding='utf-8-sig')csvdatadf.info()读取新冠疫情在2020/0101之前的数据:
beforedf=pd.read_csv(r'D:\Python\03DataAcquisition\COVID-19\before20201111.csv',encoding='utf-8-sig')
beforedf.info()
将两组数据合并:
tempalldf=beforedf.append(csvdatadf)tempalldf.head()
三、处理港澳台数据
如图所示:要将Country_Region从Hong Kong变成China。澳门和台湾也是如此:
查找有关台湾的数据:
beforedf.loc[beforedf['Country/Region']=='Taiwan']beforedf.loc[beforedf['Country/Region'].str.contains('Taiwan')]beforedf.loc[beforedf['Country/Region'].str.contains('Taiwan'),'Province/State']='Taiwan'beforedf.loc[beforedf['Province/State']=='Taiwan','Country/Region']='China'beforedf.loc[beforedf['Province/State']=='Taiwan']香港的数据处理:
beforedf.loc[beforedf['Country/Region'].str.contains('Hong Kong'),'Province/State']='Hong Kong'beforedf.loc[beforedf['Province/State']=='Hong Kong','Country/Region']='China'afterdf.loc[afterdf['Country_Region'].str.contains('Hong Kong'),'Province_State']='Hong Kong'afterdf.loc[afterdf['Province_State']=='Hong Kong','Country_Region']='China'澳门的数据处理:
beforedf.loc[beforedf['Country/Region'].str.contains('Macau'),'Province/State']='Macau'beforedf.loc[beforedf['Province/State']=='Macau','Country/Region']='China'afterdf.loc[afterdf['Country_Region'].str.contains('Macau'),'Province_State']='Macau'afterdf.loc[afterdf['Province_State']=='Macau','Country_Region']='China'最终将整理好的数据进行保存:
beforedf.to_csv("beforedf0314.csv",index=None,encoding='utf-8-sig')afterdf.to_csv("afterdf0314.csv",index=None,encoding='utf-8-sig')读到这里,这篇"Python怎么实现Excel文件的合并"文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注行业资讯频道。
数据
文件
处理
函数
文章
目录
内容
数据处理
台湾
澳门
妥当
万条
代表
功能
思路
所在
新知
时间
更多
步骤
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
怎样用云服务器登录网易云音乐
幼儿园信息网络安全工作开展情况
服务器ip入侵
数据库语言源程序是什么语言
襄阳专业的软件开发电话
媒体服务器怎么关
2k17服务器连不上
奔驰互联网科技
网络技术有网页设计吗
我的世界服务器人最多
孔雀东南飞下载软件开发
咸阳软件开发行业标准
软件开发价格定价标准
数据库互联互通是什么意思
维普数据库论文检测
newport数据库
河南服务器托管知名企业服务器
二年级视频软件开发
网络安全管理考试内容
数据库和集与差集
搬瓦工服务器管理
触碰网络安全红线
手机mt4换服务器
现代科学网络技术平台
母乳数据库建设标准
vb已有数据库创建表
mysql数据库题目
陕西学生网络安全教育视频
数据库中的实体标识符
网络安全的大招都在这里