![](/img/trans.png)
[英]How can I use python to combine data from multiple excel files into one excel file?
[英]Python: How to copy Excel worksheet from multiple Excel files to one Excel file that contains all the worksheets from other Excel files
It's my first time to use pandas, I have multiple excel files, that i want to combine all into one Excel file using python pandas.
我设法将每个 excel 文件中第一张纸的内容合并到一个新 excel 文件中的一张纸中,如下图所示:一张纸中的组合纸
我写了这段代码来实现这个:
import glob
import pandas as pd
path = "C:/folder"
file_identifier = "*.xls"
all_data = pd.DataFrame()
for f in glob.glob(path + "/*" + file_identifier):
df = pd.read_excel(f)
all_data = all_data.append(df,ignore_index=True)
writer = pd.ExcelWriter('combined.xls', engine='xlsxwriter')
all_data.to_excel(writer, sheet_name='Summary Sheet')
writer.save()
file_df = pd.read_excel("C:/folder/combined.xls")
# Keep only FIRST record from set of duplicates
file_df_first_record = file_df.drop_duplicates(subset=["Test summary", "Unnamed: 1", "Unnamed: 2",
"Unnamed: 3"], keep="first")
file_df_first_record.to_excel("filtered.xls", index=False, sheet_name='Summary Sheet')
但我有两个问题:
So i managed to combine worksheet1 from all Excel files in one sheet, but now I want to copy A, B, C, D, E worksheets into one Excel file that has all other remaining worksheets in other Excel files.
我拥有的每个 Excel 文件看起来像这个单个 excel 文件
如果您想将所有数据收集在一个工作表中,您可以使用以下脚本:
将所有要处理的excel工作簿(即excel文件)放入一个文件夹(见变量paths
)。
使用glob.glob
获取该文件夹中所有工作簿的路径。
使用read_excel(path, sheet_name=None)
返回每个工作簿的所有工作表并准备合并。
使用concat
合并所有工作表。
导出最终的 output to_excel
。
import pandas as pd import glob paths = glob.glob(r"C:\excelfiles\*.xlsx") path_save = r"finished.xlsx" df_lst = [pd.read_excel(path, sheet_name=None).values() for path in paths] df_lst = [y.transpose().reset_index().transpose() for x in df_lst for y in x] df_result = pd.concat(df_lst, ignore_index=True) df_result.to_excel(path_save, index=False, header=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.