背景: A 表内容如下, 根据 B 列字段分类, 拆分成多个表
环境配置: 需要安装 pandas,xlrd,openpyxl 库
参考教程: Pandas 之 read_excel() 和 to_excel() 函数解析 https://www.cnblogs.com/hankleo/p/11426784.html
- # 将一个 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 个文件
来源: http://www.bubuko.com/infodetail-3520219.html