Merge multiple xls files

Compiled from various sources on internet

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os, pandas as pd

# filenames
excel_names = []
for filename in os.listdir('<Dir path'):
    if filename.endswith('.xls'):
        excel_names.append(filename)
excels = []
for name in excel_names:
    try:
        excels.append(pd.ExcelFile(os.path.join('', name)))
    except IndexError:
        print(name)
    
    

# turn them into dataframes
frames = [x.parse(x.sheet_names[0], header=None,index_col=None) for x in excels]

# delete the first row for all frames except the first
# i.e. remove the header row -- assumes it's the first
frames[1:] = [df[1:] for df in frames[1:]]

# concatenate them..
combined = pd.concat(frames)

# write it out
combined.to_excel("c.xlsx", header=False, index=False)

Comments

Popular posts from this blog

Tumblr posts about sexplanation, DoeEyes and Dr. Lindsay Doe

बैगन

Type of Common Charts used in visualization