首页 > 解决方案 > 使用 pandas 将文件连接成 2 个数据帧

问题描述

我有一个包含 4 个文件的文件夹,我想将 2 个第一个文件连接到 df1 并将 2 个第二个文件连接到 df2

我找到了这种方法,但实际上它将所有文件连接到一个数据框中

fiels = #the path
df= pd.concat([pd.read_excel(fiel) for fiel in glob.glob(fiels+ "/*.xlsx")],ignore_index=True)

请问有什么建议吗?

标签: pythonpandas

解决方案


IIUC 通过索引选择文件 - 前 2 个和所有其他文件,如:

files = glob.glob(fiels+ "/*.xlsx")

df1 = pd.concat([pd.read_excel(fiel) for fiel in files[:2]],ignore_index=True)
df2 = pd.concat([pd.read_excel(fiel) for fiel in files[2:]],ignore_index=True)

推荐阅读