本文实例为大家分享了Python将一个Excel拆分为多个Excel的具体代码,供大家参考,具体内容如下
原始文档如下图所示
将销售部门一、二、三科分别存为三个Excel
代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# -*- coding: utf-8 -*- """ Created on Mon Jul 9 20:25:31 2018 @author: Lenovo """ import pandas as pd data = pd.read_excel( "E:\data1.xls" ) rows = data.shape[ 0 ] #获取行数 shape[1]获取列数 department_list = [] for i in range (rows): temp = data[ "销售部门" ][i] if temp not in department_list: department_list.append(temp) #将销售部门的分类存在一个列表中 for department in department_list: new_df = pd.DataFrame() for i in range ( 0 , rows): if data[ "销售部门" ][i] = = department: new_df = pd.concat([new_df, data.iloc[[i],:]], axis = 0 , ignore_index = True ) new_df.to_excel( str (department) + ".xls" , sheet_name = department, index = False ) #将每个销售部门存成一个新excel |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_41816368/article/details/80978885