Learner
Learner

Reputation: 682

concatenate files into 2 data frames using pandas

I have a folder with 4 files, I want to concatenate the 2 first files into df1 and the 2 second files into df2

I found this method but actually it concatenates all of the files into one dataframe

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

Any suggestions to do this please?

Upvotes: 1

Views: 31

Answers (1)

jezrael
jezrael

Reputation: 863731

IIUC select files by indexing - first 2 and all another like:

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)

Upvotes: 2

Related Questions