I am trying to loop through 8 files in a specific folder and grab the data from specific column in the first tab of each. Then, I want to paste the data into a new, consolidated file with each of the data frames on their own separate tab. This is what I have so far...
My questions:
import pandas as pd
import os
import glob
os.chdir(file path)
FileList = glob.glob('*.xlsx')
data = {}
for file in FileList:
df = pd.read_excel(file, 0, header=9)
df = df[['Project Code', 'Project', 'Location', 'Opening Balance Dollars', 'Accrued This Year Dollars',
'Opening Balance + Accrued Dollars', 'Taken Dollars', 'Closing Dollars']]
data[file] = df
To save to excel you need to have openpyxl installed. Then I would append each df to a list:
Filelist = glob.glob(r"path\*.xlsx")
list_df = []
for file in Filelist:
list_df.append(pd.read_excel(file))
# the names for each sheet in the consolidated file
names = ['sh1','sh2']
writer=pd.ExcelWriter(r"path\out.xlsx")
for i, df in enumerate(list_df):
df.to_excel(writer,sheet_name="{0}".format(names[i]),index = False)
writer.save()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.