Pandas实现批量拆分与合并Excel的示例代码
作者:qq_23605533 时间:2022-06-07 22:36:28
前言
提示:这里可以添加本文要记录的大概内容:
将一个EXCEL等份拆成多个EXCEL
将多个小EXCEL合并成一个大EXCEL并标记来源
提示:以下是本篇文章正文内容,下面案例可供参考
一、拆分成小表格
代码如下(示例):
import pandas as pd
import os
work_dir=r"G:\360Downloads\myself\zuoye\合并拆分"
splits_dir=f"{work_dir}\splits"
#如果不存在splits文件夹则创建它
if not os.path.exists(splits_dir):
os.mkdir(splits_dir)
#引入源文件
df_source=pd.read_excel(f"{work_dir}/5月份台账.xlsx",sheet_name="5月份台账87334",skiprows=2)
df_source.head(3)
df_source.index
df_source.shape
total_row_count=df_source.shape[0]
total_row_count
#拆分成多个大小相同的excel
#1.使用df.iloc方法
#2.使用dataframe.to_excel保存到每个小excel中
#计算拆分后小excel的行数
user_names=["xiaoA","xiaoB","xiaoC","wmy","jzz","xmw"]
#每个人的任务数
split_size=total_row_count//len(user_names)
if total_row_count%len(user_names)!=0:
split_size+=1
split_size
#拆分多个dataframe
df_subs=[]
#enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列
for idx,user_name in enumerate(user_names):
#iloc的开始索引
begin=idx*split_size
#iloc的结束索引
end=begin+split_size
#实现df按照iloc拆分
df_sub=df_source.iloc[begin:end]
#将每个子df存入列表
df_subs.append((idx,user_name,df_sub))
#将每个dataframe存入excel
for idx,user_name,df_sub in df_subs:
filename=f"{splits_dir}/ee_{idx}_{user_name}.xlsx"
df_sub.to_excel(filename,index=False)
二、合并excel
1.介绍
1、遍历文件夹,得到要合并的excel文件列表
2、分别读取到dataframe,给每一列标记来源
3、使用pd.conca进行df批量合并
4、将合并后的dataframe输出到excel
2.代码
代码如下(示例):
import pandas as pd
import os
work_dir=r"G:\360Downloads\myself\zuoye\合并拆分"
splits_dir=f"{work_dir}\splits"
#如果不存在splits文件夹则创建它
if not os.path.exists(splits_dir):
os.mkdir(splits_dir)
#遍历文件夹,得到要合并的excel名称列表
excel_names=[]
for excel_name in os.listdir(splits_dir):
excel_names.append(excel_name)
excel_names
df_list=[]
for excel_name in excel_names:
#读取每个excel到df
excel_path=f"{splits_dir}/{excel_name}"
df_split=pd.read_excel(excel_path)
username=excel_name.replace("ee_","").replace(".xlsx","")[2:]
print(excel_name,username)
#添加列,用户名字
df_split["username"]=username
df_list.append(df_split)
#concat合并
df_merged=pd.concat(df_list)
df_merged.shape
df_merged.head()
df_merged["username"].value_counts()
#输出
df_merged.to_excel(f"{work_dir}/merged.xlsx",index=False)
来源:https://blog.csdn.net/qq_23605533/article/details/124669369
标签:Pandas,拆分,合并,Excel
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
产品设计与用户体验
2009-02-02 10:15:00
python实现批量修改文件名
2022-03-10 18:44:54
![](https://img.aspxhome.com/file/2023/3/127833_0s.jpg)
Vue中的侦听器及使用场景
2024-05-09 15:26:05
Django中使用Celery的教程详解
2023-07-12 05:08:35
![](https://img.aspxhome.com/file/2023/7/91927_0s.png)
SqlServer 垂直分表(减少程序改动)
2024-01-27 05:29:41
SQL语句分组获取记录的第一条数据的方法
2024-01-12 17:23:13
![](https://img.aspxhome.com/file/2023/4/94414_0s.jpg)
python给视频添加背景音乐并改变音量的具体方法
2021-01-26 20:18:47
Gradio机器学习模型快速部署工具应用分享
2023-06-30 01:33:33
![](https://img.aspxhome.com/file/2023/6/62846_0s.jpg)
python构造IP报文实例
2023-07-10 20:40:51
防止web项目中的SQL注入
2024-01-26 00:44:25
c#实现数据库事务示例分享
2024-01-28 05:03:02
JS基于面向对象实现的选项卡效果示例
2024-04-19 10:42:56
vue中Axios的封装与API接口的管理详解
2024-05-02 16:23:39
![](https://img.aspxhome.com/file/2023/8/130838_0s.jpg)
简单了解python中对象的取反运算符
2021-04-21 16:50:57
![](https://img.aspxhome.com/file/2023/5/82955_0s.jpg)
asp中的rs.open与conn.execute的区别说明
2011-02-24 10:56:00
如何用SQLMail建立一个电子刊物自动处理系统?
2010-06-13 14:36:00
golang中cache组件的使用及groupcache源码解析
2024-02-07 11:12:25
SQL Server误区30日谈 第13天 在SQL Server 2000兼容模式下不能使用DMV
2024-01-13 18:34:25
SQLServer2008新实例远程数据库链接问题(sp_addlinkedserver)
2024-01-19 23:44:22
Python numpy.interp的实例详解
2023-01-21 07:07:06
![](https://img.aspxhome.com/file/2023/0/89330_0s.png)