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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:41:42  来源:igfitidea点击:

Append Multiple Excel Files(xlsx) together in python

pythonexcelpandasmergedata-science

提问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_databe 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 mapversion of the forloop 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.

或者其他答案中显示的列表理解方法。

回答by jezrael

Use list comprehension+ concat:

使用list comprehension+ concat

all_data = [pd.read_excel(f) for f in glob.glob("output/test/*.xlsx")]
df = pd.concat(all_data, ignore_index=True)