Python使用pandas对Exccel按某列分割成多个文件

首先需要安装第三方库:

pip3 install pandas

pip3 install xlrd

pip3 install openpyxl

后面这两个不晓得是啥,反正是在运行中报错提示,然后安装的。

目标文件在E盘下面:data1.xlsx,文件内容:

  

import pandas as pd

io = "e:/data1.xlsx"    #目标文件
data = pd.read_excel(io,0)

rows = data.shape[0]  # 获取行数 shape[1]获取列数
target_col = "name"   # 要分割的列
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"E:/fenge/"+ str(col) + ".xlsx", sheet_name=col, index=False)  # 将分割的存成一个新excel

运行就会将指定列分割成多个文件:

后面说一下感慨吧!这尼玛太慢了吧,我一个100多M的文件,分成九个,半个小时了还没反应····· 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章