Merging of Excel Files with Pandas
up vote
-2
down vote
favorite
I have about 19 different excel files that all have the same columns and I want to merge them all together into one excel file. When I go merge it, half way down, the columns shift one to the right.
For example all the columns are A through U but when merged some will be A through U and some will be A through V. Everything lines up except the U column it either skips the U column and put everything in V or adds something random to the U column and pushes what is supposed to be in U into V.
The code I have to merge them is as follow:
def mergingExcel():
sharedDocs = "C:\Users\CHI\Documents\SPSharedDocuments\*.xlsx"
invoices = "C:\UsersCHI\Documents\SPInvoices\*.xlsx"
formsCerts = "C:\UsersCHI\Documents\SPForms&Certificates\*.xlsx"
mgmt = "C:\UsersCHI\Documents\SPManagement\*.xlsx"
files = [sharedDocs, invoices, formsCerts, mgmt] #contains variables that point to the path of files
for docs in files: #cycles through the files array
excel =
for file in glob.glob(docs): #cycles through the excel files at each path
excel.append(file) #add excel doc to excel array
excels = [pd.ExcelFile(name) for name in excel]
frames = [x.parse(x.sheet_names[0], header=None, index_col=None) for x in excels]
frames[1:] = [df[1:] for df in frames[1:]]
combined = pd.concat(frames)
if sharedDocs == docs:
combined.to_excel("SharedDocsMerged.xlsx", header = False, index = False)
shutil.move(os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\SharedDocsMerged.xlsx","C:\Users\CHI\Documents\SPSharedDocuments"),os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\SharedDocsMerged.xlsx","C:\Users\CHI\Documents\SPSharedDocuments"))
elif invoices == docs:
combined.to_excel("InvoicesMerged.xlsx", header = False, index = False)
shutil.move(os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\InvoicesMerged.xlsx","C:\Users\CHI\Documents\SPInvoices"),os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\InvoicesMerged.xlsx","C:\Users\CHI\Documents\SPInvoices"))
elif formsCerts == docs:
combined.to_excel("FormsCertsMerged.xlsx", header = False, index = False)
shutil.move(os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\FormsCertsMerged.xlsx","C:\Users\CHI\Documents\SPForms&Certificates"),os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\FormsCertsMerged.xlsx","C:\Users\CHI\Documents\SPForms&Certificates"))
else:
combined.to_excel("MGMTMerged.xlsx", header = False, index = False)
shutil.move(os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\MGMTMerged.xlsx","C:\Users\CHI\Documents\SPManagement"),os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\MGMTMerged.xlsx","C:\Users\CHI\Documents\SPManagement"))
return;
Thank in advance for any advice
python excel pandas
add a comment |
up vote
-2
down vote
favorite
I have about 19 different excel files that all have the same columns and I want to merge them all together into one excel file. When I go merge it, half way down, the columns shift one to the right.
For example all the columns are A through U but when merged some will be A through U and some will be A through V. Everything lines up except the U column it either skips the U column and put everything in V or adds something random to the U column and pushes what is supposed to be in U into V.
The code I have to merge them is as follow:
def mergingExcel():
sharedDocs = "C:\Users\CHI\Documents\SPSharedDocuments\*.xlsx"
invoices = "C:\UsersCHI\Documents\SPInvoices\*.xlsx"
formsCerts = "C:\UsersCHI\Documents\SPForms&Certificates\*.xlsx"
mgmt = "C:\UsersCHI\Documents\SPManagement\*.xlsx"
files = [sharedDocs, invoices, formsCerts, mgmt] #contains variables that point to the path of files
for docs in files: #cycles through the files array
excel =
for file in glob.glob(docs): #cycles through the excel files at each path
excel.append(file) #add excel doc to excel array
excels = [pd.ExcelFile(name) for name in excel]
frames = [x.parse(x.sheet_names[0], header=None, index_col=None) for x in excels]
frames[1:] = [df[1:] for df in frames[1:]]
combined = pd.concat(frames)
if sharedDocs == docs:
combined.to_excel("SharedDocsMerged.xlsx", header = False, index = False)
shutil.move(os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\SharedDocsMerged.xlsx","C:\Users\CHI\Documents\SPSharedDocuments"),os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\SharedDocsMerged.xlsx","C:\Users\CHI\Documents\SPSharedDocuments"))
elif invoices == docs:
combined.to_excel("InvoicesMerged.xlsx", header = False, index = False)
shutil.move(os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\InvoicesMerged.xlsx","C:\Users\CHI\Documents\SPInvoices"),os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\InvoicesMerged.xlsx","C:\Users\CHI\Documents\SPInvoices"))
elif formsCerts == docs:
combined.to_excel("FormsCertsMerged.xlsx", header = False, index = False)
shutil.move(os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\FormsCertsMerged.xlsx","C:\Users\CHI\Documents\SPForms&Certificates"),os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\FormsCertsMerged.xlsx","C:\Users\CHI\Documents\SPForms&Certificates"))
else:
combined.to_excel("MGMTMerged.xlsx", header = False, index = False)
shutil.move(os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\MGMTMerged.xlsx","C:\Users\CHI\Documents\SPManagement"),os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\MGMTMerged.xlsx","C:\Users\CHI\Documents\SPManagement"))
return;
Thank in advance for any advice
python excel pandas
5
it's really hard to know what the issue here is without some sample data
– wpercy
Nov 9 at 21:05
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I have about 19 different excel files that all have the same columns and I want to merge them all together into one excel file. When I go merge it, half way down, the columns shift one to the right.
For example all the columns are A through U but when merged some will be A through U and some will be A through V. Everything lines up except the U column it either skips the U column and put everything in V or adds something random to the U column and pushes what is supposed to be in U into V.
The code I have to merge them is as follow:
def mergingExcel():
sharedDocs = "C:\Users\CHI\Documents\SPSharedDocuments\*.xlsx"
invoices = "C:\UsersCHI\Documents\SPInvoices\*.xlsx"
formsCerts = "C:\UsersCHI\Documents\SPForms&Certificates\*.xlsx"
mgmt = "C:\UsersCHI\Documents\SPManagement\*.xlsx"
files = [sharedDocs, invoices, formsCerts, mgmt] #contains variables that point to the path of files
for docs in files: #cycles through the files array
excel =
for file in glob.glob(docs): #cycles through the excel files at each path
excel.append(file) #add excel doc to excel array
excels = [pd.ExcelFile(name) for name in excel]
frames = [x.parse(x.sheet_names[0], header=None, index_col=None) for x in excels]
frames[1:] = [df[1:] for df in frames[1:]]
combined = pd.concat(frames)
if sharedDocs == docs:
combined.to_excel("SharedDocsMerged.xlsx", header = False, index = False)
shutil.move(os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\SharedDocsMerged.xlsx","C:\Users\CHI\Documents\SPSharedDocuments"),os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\SharedDocsMerged.xlsx","C:\Users\CHI\Documents\SPSharedDocuments"))
elif invoices == docs:
combined.to_excel("InvoicesMerged.xlsx", header = False, index = False)
shutil.move(os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\InvoicesMerged.xlsx","C:\Users\CHI\Documents\SPInvoices"),os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\InvoicesMerged.xlsx","C:\Users\CHI\Documents\SPInvoices"))
elif formsCerts == docs:
combined.to_excel("FormsCertsMerged.xlsx", header = False, index = False)
shutil.move(os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\FormsCertsMerged.xlsx","C:\Users\CHI\Documents\SPForms&Certificates"),os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\FormsCertsMerged.xlsx","C:\Users\CHI\Documents\SPForms&Certificates"))
else:
combined.to_excel("MGMTMerged.xlsx", header = False, index = False)
shutil.move(os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\MGMTMerged.xlsx","C:\Users\CHI\Documents\SPManagement"),os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\MGMTMerged.xlsx","C:\Users\CHI\Documents\SPManagement"))
return;
Thank in advance for any advice
python excel pandas
I have about 19 different excel files that all have the same columns and I want to merge them all together into one excel file. When I go merge it, half way down, the columns shift one to the right.
For example all the columns are A through U but when merged some will be A through U and some will be A through V. Everything lines up except the U column it either skips the U column and put everything in V or adds something random to the U column and pushes what is supposed to be in U into V.
The code I have to merge them is as follow:
def mergingExcel():
sharedDocs = "C:\Users\CHI\Documents\SPSharedDocuments\*.xlsx"
invoices = "C:\UsersCHI\Documents\SPInvoices\*.xlsx"
formsCerts = "C:\UsersCHI\Documents\SPForms&Certificates\*.xlsx"
mgmt = "C:\UsersCHI\Documents\SPManagement\*.xlsx"
files = [sharedDocs, invoices, formsCerts, mgmt] #contains variables that point to the path of files
for docs in files: #cycles through the files array
excel =
for file in glob.glob(docs): #cycles through the excel files at each path
excel.append(file) #add excel doc to excel array
excels = [pd.ExcelFile(name) for name in excel]
frames = [x.parse(x.sheet_names[0], header=None, index_col=None) for x in excels]
frames[1:] = [df[1:] for df in frames[1:]]
combined = pd.concat(frames)
if sharedDocs == docs:
combined.to_excel("SharedDocsMerged.xlsx", header = False, index = False)
shutil.move(os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\SharedDocsMerged.xlsx","C:\Users\CHI\Documents\SPSharedDocuments"),os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\SharedDocsMerged.xlsx","C:\Users\CHI\Documents\SPSharedDocuments"))
elif invoices == docs:
combined.to_excel("InvoicesMerged.xlsx", header = False, index = False)
shutil.move(os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\InvoicesMerged.xlsx","C:\Users\CHI\Documents\SPInvoices"),os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\InvoicesMerged.xlsx","C:\Users\CHI\Documents\SPInvoices"))
elif formsCerts == docs:
combined.to_excel("FormsCertsMerged.xlsx", header = False, index = False)
shutil.move(os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\FormsCertsMerged.xlsx","C:\Users\CHI\Documents\SPForms&Certificates"),os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\FormsCertsMerged.xlsx","C:\Users\CHI\Documents\SPForms&Certificates"))
else:
combined.to_excel("MGMTMerged.xlsx", header = False, index = False)
shutil.move(os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\MGMTMerged.xlsx","C:\Users\CHI\Documents\SPManagement"),os.path.join("C:\Users\CHI\source\repos\PythonApplication1\PythonApplication1\MGMTMerged.xlsx","C:\Users\CHI\Documents\SPManagement"))
return;
Thank in advance for any advice
python excel pandas
python excel pandas
edited Nov 9 at 21:08
petezurich
3,37881633
3,37881633
asked Nov 9 at 21:03
jordan23
237
237
5
it's really hard to know what the issue here is without some sample data
– wpercy
Nov 9 at 21:05
add a comment |
5
it's really hard to know what the issue here is without some sample data
– wpercy
Nov 9 at 21:05
5
5
it's really hard to know what the issue here is without some sample data
– wpercy
Nov 9 at 21:05
it's really hard to know what the issue here is without some sample data
– wpercy
Nov 9 at 21:05
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233242%2fmerging-of-excel-files-with-pandas%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
it's really hard to know what the issue here is without some sample data
– wpercy
Nov 9 at 21:05