利用python Pandas實(shí)現(xiàn)批量拆分Excel與合并Excel
一、實(shí)例演示
1.將一個(gè)大Excel等份拆成多個(gè)Excel
2.將多個(gè)小Excel合并成一個(gè)大Excel并標(biāo)記來(lái)源
work_dir="./course_datas/c15_excel_split_merge"
splits_dir=f"{work_dir}/splits"
import os
if not os.path.exists(splits_dir):
os.mkdir(splits_dir)
二、讀取源Excel到Pandas
import pandas as pd No output
df_source = pd.read_excel(f"{work_dir}/crazyant_blog_articles_source.xlsx")
No output
df_source.head()
id title tags 0 2585 Tensorflow怎樣接收變長(zhǎng)列表特征 python,tensorflow,特征工程 1 2583 Pandas實(shí)現(xiàn)數(shù)據(jù)的合并concat pandas,python,數(shù)據(jù)分析 2 2574 Pandas的Index索引有什么用途? pandas,python,數(shù)據(jù)分析 3 2564 機(jī)器學(xué)習(xí)常用數(shù)據(jù)集大全 python,機(jī)器學(xué)習(xí) 4 2561 一個(gè)數(shù)據(jù)科學(xué)家的修煉路徑 數(shù)據(jù)分析
df_source.index
RangeIndex(start=0, stop=258, step=1)
df_source.shape
(258, 3)
total_row_count = df_source.shape[0] total_row_count
258
三、將一個(gè)大Excel等份拆成多個(gè)Excel
1.使用df.iloc方法,將一個(gè)大的dataframe,拆分成多個(gè)小dataframe
2.將使用dataframe.to_excel保存每個(gè)小Excel
1、計(jì)算拆分后的每個(gè)excel的行數(shù)
# 這個(gè)大excel,會(huì)拆分給這幾個(gè)人 user_names = ["xiao_shuai", "xiao_wang", "xiao_ming", "xiao_lei", "xiao_bo", "xiao_hong"] No output
# 每個(gè)人的任務(wù)數(shù)目
split_size = total_row_count // len(user_names)
if total_row_count % len(user_names) != 0:
split_size += 1
split_size
43
2、拆分成多個(gè)dataframe
df_subs = []
for idx, user_name in enumerate(user_names):
# iloc的開(kāi)始索引
begin = idx*split_size
# iloc的結(jié)束索引
end = begin+split_size
# 實(shí)現(xiàn)df按照iloc拆分
df_sub = df_source.iloc[begin:end]
# 將每個(gè)子df存入列表
df_subs.append((idx, user_name, df_sub))
No output
3、將每個(gè)datafame存入excel
for idx, user_name, df_sub in df_subs:
file_name = f"{splits_dir}/crazyant_blog_articles_{idx}_{user_name}.xlsx"
df_sub.to_excel(file_name, index=False)
No output
四、合并多個(gè)小Excel到一個(gè)大Excel
1.遍歷文件夾,得到要合并的Excel文件列表
2.分別讀取到dataframe,給每個(gè)df添加一列用于標(biāo)記來(lái)源
3.使用pd.concat進(jìn)行df批量合并
4.將合并后的dataframe輸出到excel
1. 遍歷文件夾,得到要合并的Excel名稱(chēng)列表
import os
excel_names = []
for excel_name in os.listdir(splits_dir):
excel_names.append(excel_name)
excel_names
['crazyant_blog_articles_0_xiao_shuai.xlsx',
'crazyant_blog_articles_1_xiao_wang.xlsx',
'crazyant_blog_articles_2_xiao_ming.xlsx',
'crazyant_blog_articles_3_xiao_lei.xlsx',
'crazyant_blog_articles_4_xiao_bo.xlsx',
'crazyant_blog_articles_5_xiao_hong.xlsx']
2. 分別讀取到dataframe
df_list = []
for excel_name in excel_names:
# 讀取每個(gè)excel到df
excel_path = f"{splits_dir}/{excel_name}"
df_split = pd.read_excel(excel_path)
# 得到username
username = excel_name.replace("crazyant_blog_articles_", "").replace(".xlsx", "")[2:]
print(excel_name, username)
# 給每個(gè)df添加1列,即用戶(hù)名字
df_split["username"] = username
df_list.append(df_split)
crazyant_blog_articles_0_xiao_shuai.xlsx xiao_shuai
crazyant_blog_articles_1_xiao_wang.xlsx xiao_wang
crazyant_blog_articles_2_xiao_ming.xlsx xiao_ming
crazyant_blog_articles_3_xiao_lei.xlsx xiao_lei
crazyant_blog_articles_4_xiao_bo.xlsx xiao_bo
crazyant_blog_articles_5_xiao_hong.xlsx xiao_hong
3. 使用pd.concat進(jìn)行合并
df_merged = pd.concat(df_list) No output
df_merged.shape
(258, 4)
df_merged.head()
id title tags username
0 2585 Tensorflow怎樣接收變長(zhǎng)列表特征 python,tensorflow,特征工程 xiao_shuai
1 2583 Pandas實(shí)現(xiàn)數(shù)據(jù)的合并concat pandas,python,數(shù)據(jù)分析 xiao_shuai
2 2574 Pandas的Index索引有什么用途? pandas,python,數(shù)據(jù)分析 xiao_shuai
3 2564 機(jī)器學(xué)習(xí)常用數(shù)據(jù)集大全 python,機(jī)器學(xué)習(xí) xiao_shuai
4 2561 一個(gè)數(shù)據(jù)科學(xué)家的修煉路徑 數(shù)據(jù)分析 xiao_shuai
df_merged["username"].value_counts()
xiao_hong 43
xiao_bo 43
xiao_shuai 43
xiao_lei 43
xiao_wang 43
xiao_ming 43
Name: username, dtype: int64
xiao_hong 43xiao_bo 43xiao_shuai 43xiao_lei 43xiao_wang 43xiao_ming 43Name: username, dtype: int64
4. 將合并后的dataframe輸出到excel
df_merged.to_excel(f"{work_dir}/crazyant_blog_articles_merged.xlsx", index=False)
到此這篇關(guān)于利用python Pandas實(shí)現(xiàn)批量拆分Excel與合并Excel的文章就介紹到這了,更多相關(guān)Pandas批量拆分Excel與合并Excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python爬蟲(chóng)實(shí)現(xiàn)抓取京東店鋪信息及下載圖片功能示例
這篇文章主要介紹了Python爬蟲(chóng)實(shí)現(xiàn)抓取京東店鋪信息及下載圖片功能,涉及Python頁(yè)面請(qǐng)求、響應(yīng)、解析等相關(guān)操作技巧,需要的朋友可以參考下2018-08-08
基于Python實(shí)現(xiàn)地標(biāo)景點(diǎn)識(shí)別功能
地標(biāo)景點(diǎn)識(shí)別是一種基于計(jì)算機(jī)視覺(jué)技術(shù)的應(yīng)用,旨在通過(guò)對(duì)圖像進(jìn)行分析和處理,自動(dòng)識(shí)別出圖片中的地標(biāo)景點(diǎn),本文將介紹地標(biāo)景點(diǎn)識(shí)別的背景和原理,并使用Python編程語(yǔ)言來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的地標(biāo)景點(diǎn)識(shí)別系統(tǒng),感興趣的朋友可以參考下2024-01-01
Python利用Pytorch實(shí)現(xiàn)繪制ROC與PR曲線(xiàn)圖
這篇文章主要和大家分享一下Python利用Pytorch實(shí)現(xiàn)繪制ROC與PR曲線(xiàn)圖的相關(guān)代碼,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2022-12-12
詳解如何使用opencv實(shí)現(xiàn)圖片相似度檢測(cè)
這篇文章主要為大家詳細(xì)介紹了如何使用opencv實(shí)現(xiàn)圖片相似度檢測(cè),文中的示例代碼講解詳細(xì),對(duì)于我們學(xué)習(xí)人工智能有一定的幫助,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
Python?pandas中apply函數(shù)簡(jiǎn)介以及用法詳解
apply()函數(shù)是pandas里面所有函數(shù)中自由度最高的函數(shù), apply()函數(shù)的參數(shù)是一個(gè)函數(shù)指針,這里可以使用lambda表達(dá)式幫助簡(jiǎn)化代碼,下面這篇文章主要給大家介紹了關(guān)于Python?pandas中apply函數(shù)簡(jiǎn)介以及用法的相關(guān)資料,需要的朋友可以參考下2022-09-09
pandas 時(shí)間格式轉(zhuǎn)換的實(shí)現(xiàn)
這篇文章主要介紹了pandas 時(shí)間格式轉(zhuǎn)換的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

