在python中一起附加多个Excel文件(xlsx) [英] Append Multiple Excel Files(xlsx) together in python
本文介绍了在python中一起附加多个Excel文件(xlsx)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import pandas as pd
import os
import glob
all_data = pd.DataFrame()
for f in glob.glob("output/test*.xlsx")
df = pd.read_excel(f)
all_data = all_data.append(df, ignore_index=True)
我想将多个xlsx文件放入一个xlsx中. excel文件位于output/test文件夹中.列是相同的,除了我想要的所有行.上面的代码似乎不起作用
I want to put multiple xlsx files into one xlsx. the excel files are in the output/test folder. The columns are the same, in all but I want concat the rows. the above code doesn't seem to work
推荐答案
让all_data
成为列表.
all_data = []
for f in glob.glob("output/test/*.xlsx"):
all_data.append(pd.read_excel(f))
现在,拨打pd.concat
:
df = pd.concat(all_data, ignore_index=True)
确保所有列名都相同,否则此解决方案将无法正常工作.
Make sure all column names are the same, otherwise this solution won't work.
您还可以使用上述for
循环的map
版本:
You could also use a map
version of the for
loop above:
g = map(pd.read_excel, glob.glob("output/test/*.xlsx"))
df = pd.concat(list(g), ignore_index=True)
或列表理解方法,如其他答案所示.
Or the list comprhension method as shown in the other answer.
这篇关于在python中一起附加多个Excel文件(xlsx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文