pandas 在python中将多个Excel文件(xlsx)附加在一起
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46930575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Append Multiple Excel Files(xlsx) together in python
提问by user3821872
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)
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
我想将多个 xlsx 文件放入一个 xlsx。excel 文件位于 output/test 文件夹中。列是相同的,但我想连接行。上面的代码似乎不起作用
回答by cs95
Let all_data
be a list.
让我们all_data
列个清单。
all_data = []
for f in glob.glob("output/test/*.xlsx"):
all_data.append(pd.read_excel(f))
Now, call pd.concat
:
现在,调用pd.concat
:
df = pd.concat(all_data, ignore_index=True)
Make sure all column names are the same, otherwise this solution won't work.
确保所有列名称都相同,否则此解决方案将不起作用。
You could also use a map
version of the for
loop above:
您还可以使用map
上述for
循环的一个版本:
g = map(pd.read_excel, glob.glob("output/test/*.xlsx"))
df = pd.concat(list(g), ignore_index=True)
Or the list comprhensionmethod as shown in the other answer.
或者其他答案中显示的列表理解方法。