首先需要安裝第三方庫:
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的文件,分成九個,半個小時了還沒反應·····