jakecp
jakecp

Reputation: 1

Adding data to an existing excel table

What I'm trying to accomplish:

What I've accomplished thus far:

My issue:

I have not yet coded anything to move the new files to a new folder but I don't foresee that being an issue. I am a beginner and am just learning python, so go easy. Any help would be very much appreciated. Thank you!

import glob
import pandas as pd
# File locations
location = "T:\\Example\\Test\\*.xlsx"
excel_files = glob.glob(location)
master = "T:\\File\\file.xlsx"
# Create data frames and concatenate files to master
df1 = pd.DataFrame()
df3 = pd.read_excel(master)
for excel_file in excel_files:
df2 = pd.read_excel(excel_file) 
df1 = pd.concat([df1, df2])
df4 = pd.concat([df3, df1])
df4.to_excel("T:\\File\\file.xlsx", index=False)

Upvotes: 0

Views: 915

Answers (1)

pyaj
pyaj

Reputation: 640

import os
import glob
import pandas as pd

# define relative path to folder containing excel data
location = "T:\\Example\\Test\\"

# load all excel files in one list
df_list = []
for file in glob.glob(os.path.join(location, "*.xlsx")):
    df = pd.read_excel(file)
    df_list.append(df)

# concatenate df_list to one big excel file
big_df = pd.concat(df_list, axis=1)

#save dataframe
big_df.to_excel("T:\\File\\file.xlsx", index=False)

Upvotes: 1

Related Questions