背景:A表内容如下,根据B列字段分类,拆分成多个表
环境配置:需要安装pandas,xlrd,openpyxl库
参考教程:Pandas之read_excel()和to_excel()函数解析
#将一个excel表根据某列字段拆分为多个工作表 import pandas as pd io="C:/.../_all.xlsx" #A表路径 data=pd.read_excel(io,0) rows=data.shape[0] #获取行数,shape[1]获取列数 target_col="Business Process" #要拆分的列 cols_list=[] for i in range(rows): temp=data[target_col][i] if temp not in cols_list: cols_list.append(temp) #同一类放在一个列表中 for col in cols_list: new_df=pd.DataFrame() for i in range(0,rows): if data[target_col][i]==col: new_df=pd.concat([new_df,data.iloc[[i],:]],axis=0,ignore_index=True) #保存新文件 new_df.to_excel(r"C:/.../1/" +str(col)+".xlsx",sheet_name=col,index=False)
拆分完成,总共有6类,拆分成6个文件
【练习读写excel文件】根据A表的某一字段将相同的类拆分成多个表
原文:https://www.cnblogs.com/xiaopc/p/12767241.html