[英]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.