简体   繁体   English

通过python将多个excel文件合并成一个excel文件

[英]Join multiple excel files into one excel file via python

I need to combine all the excel files in my directory into one excel file.我需要将我目录中的所有 excel 文件合并为一个 excel 文件。 for example, I've 3 excel files:例如,我有 3 个 excel 文件:

file1:文件 1:

在此处输入图片说明

file 2:文件2:

在此处输入图片说明

file 3:文件3:

在此处输入图片说明

I need to concatenate them and get an output as follows我需要连接它们并获得如下输出

在此处输入图片说明

but instead, they were appended one after another this is my code :但相反,它们一个接一个地附加,这是我的代码:

import pandas as pd
import numpy as np
import glob

all_data = pd.DataFrame()
for f in glob.glob('C:/Users/test-results/FinalResult/05-01-2019/*.xlsx'):
   df = pd.read_excel(f)
   all_data = all_data.append(df, ignore_index=True)

writer = pd.ExcelWriter('mycollected_data.xlsx', engine='xlsxwriter')
all_data.to_excel(writer, sheet_name='Sheet1')
writer.save()

during my quest, all I found was how to append dfs,as shown in my code and I didn't figure out how too use join在我的探索过程中,我发现的只是如何附加 dfs,如我的代码所示,但我不知道如何使用join

尝试这个:

all_data = pd.concat([all_data,df],axis=1)

You could use你可以用

files = glob.glob('C:/Users/test-results/FinalResult/05-01-2019/*.xlsx')
dfs = (pd.read_excel(f, index_col=0) for f in files)
all_data = pd.concat(dfs, axis=1)
all_data = all_data.merge(df, on = ['first_column_name'], how = 'left')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python: How to copy Excel worksheet from multiple Excel files to one Excel file that contains all the worksheets from other Excel files - Python: How to copy Excel worksheet from multiple Excel files to one Excel file that contains all the worksheets from other Excel files 使用Python将多个输入文件转换为一个Excel文件 - Convert multiple input files into one single Excel file, with Python 使用Python将多个Excel文件合并为一个主文件 - Combine multiple excel files into one master file using Python Python:如何将多个 Excel 文件合并(合并)到一个文件中? (不附加) - Python: How to Combine (concat) Multiple Excel Files into One File? (Not append) 如何通过 python 合并多个 excel 文件 - How to merge multiple excel file via python 如何使用python将多个Excel文件中的数据合并到一个Excel文件中? - How can I use python to combine data from multiple excel files into one excel file? 将具有不同行的多个Excel文件合并到一个熊猫中的Excel文件中 - Merge multiple Excel files with varied rows into one Excel file in pandas Python:将1个Excel文件按行拆分成多个Excel文件 - Python : Split 1 Excel File into multiple Excel files by rows 如何使用Python将多个excel文件动态制作成一个包含多张工作表的文件 - How to make multiple excel files into one file with multiple sheets dynamically with Python 将多个 Excel 文件转换为一个 - Converting Multiple Excel Files Into One
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM