-
Notifications
You must be signed in to change notification settings - Fork 0
/
Iterate_excels_and_combine.py
66 lines (49 loc) · 1.66 KB
/
Iterate_excels_and_combine.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#https://stackoverflow.com/questions/15793349/how-to-concatenate-three-excels-files-xlsx-using-python
import pandas as pd
import numpy as np
import glob
all_data = pd.DataFrame()
for f in glob.glob('myfolder/mydata*.xlsx'):
df = pd.read_excel(f)
all_data = all_data.append(df, ignore_index=True)
writer = pd.ExcelWriter('mycollected_data.xlsx', engine='xlsxwriter')
all_data.to_excel(writer, sheet_name='Sheet1')
writer.save()
#https://stackoverflow.com/questions/46930575/append-multiple-excel-filesxlsx-together-in-python
#method 1: list and pd.concat
all_data = []
for f in glob.glob("output/test/*.xlsx"):
all_data.append(pd.read_excel(f))
df = pd.concat(all_data, ignore_index=True)
#method 2: map and for loop
g = map(pd.read_excel, glob.glob("output/test/*.xlsx"))
df = pd.concat(list(g), ignore_index=True)
#method 3: list comprehension + concat
all_data = [pd.read_excel(f) for f in glob.glob("output/test/*.xlsx")]
df = pd.concat(all_data, ignore_index=True)
#https://stackoverflow.com/questions/20908018/import-multiple-excel-files-into-python-pandas-and-concatenate-them-into-one-dat
#method 1
import numpy as np
import pandas as pd
import glob
all_data = pd.DataFrame()
for f in glob.glob("*.xlsx"):
df = pd.read_excel(f)
all_data = all_data.append(df,ignore_index=True)
# now save the data frame
writer = pd.ExcelWriter('output.xlsx')
all_data.to_excel(writer,'sheet1')
writer.save()
#method 2
import os
import pandas as pd
path = os.getcwd()
files = os.listdir(path)
files
files_xls = [f for f in files if f[-3:] == 'xls']
files_xls
df = pd.DataFrame()
for f in files_xls:
data = pd.read_excel(f, 'Sheet1')
df = df.append(data)
df