Python pandas依列拆分为多个Excel文件

2022-10-25 13:52:53 浏览数 (1)

问题:Python pandas依列拆分为多个Excel文件

实例:下面成绩表中按“班别”拆分为多个工作簿,一个班一个文件

====代码====

代码语言:javascript复制
import pandas as pd
data = pd.read_excel("D:yhd_pythonyhd-python依列拆分Excel汇总.xlsx")
rows = data.shape[0] #获取行数 shape[1]获取列数
print(rows)
data["身份证"]=[" %i"%i for i in data["身份证"]]
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)
    #print(new_df)        
    new_df.to_excel(str(department) ".xls", index = False)

====效果====

===每个文件如下===

0 人点赞