import pandas as pd
import os
df_source = pd.read_excel(
"./pandas/ant-learn-pandas/course_datas/c15_excel_split_merge/crazyant_blog_articles_source.xlsx")
print(df_source.head(5))
if not os.path.exists("splits_dir"):
os.mkdir("splits_dir")
res = df_source.index
print(res)
total_row_count = df_source.shape[0]
print(total_row_count)
使用df.iloc方法,将一个大的dataframe,拆分成多个小dataframe
user_names = ["xiao_shuai", "xiao_wang", "xiao_ming", "xiao_lei", "xiao_bo", "xiao_hong"]
split_size = total_row_count // len(user_names)
if total_row_count % len(user_names) != 0:
split_size += 1
df_subs = list()
for idx, user_names in enumerate(user_names):
begin = idx*split_size
end = begin+split_size
df_sub = df_source.iloc[begin:end]
df_subs.append((idx, user_names, df_sub))
for idx, user_names, df_sub in df_subs:
file_name = f"splits_dir/crazyant_blog_articles_{idx}_{user_names}.xlsx"
df_sub.to_excel(file_name, index=False)
使用pd.concat进行df批量合并
excel_names = list()
for excel_name in os.listdir("splits_dir"):
excel_names.append(excel_name)
df_list = list()
for excel_name in excel_names:
excel_path = f"splits_dir/{excel_name}"
df_split = pd.read_excel(excel_path)
username = excel_name.replace("crazyant_blog_articles_", "").replace(".xlsx", "")[2:]
df_split["username"] = username
df_list.append(df_split)
df_merged = pd.concat(df_list)
print(df_merged.head(5))
df_merged.to_excel(f"splits_dir/merged_data.xlsx", index=False)