Guest User

Untitled

a guest
Feb 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. import pandas as pd
  2. import os
  3.  
  4. # set working directories for files
  5. starting_folder=('Purchasing/unformatted/')
  6. save_folder = ('Purchasing/')
  7.  
  8. #create list of excel file names from folder
  9. files = []
  10. for file in os.listdir(starting_folder):
  11. filename = os.fsdecode(file)
  12. files.append(filename)
  13.  
  14. # create list for file names to be saved as csv
  15. save_files = [w.replace('xlsx','csv') for w in files]
  16.  
  17. # create data frame of fiscal calendar
  18. calendar = pd.read_excel('Purchasing/Fiscal Calendar 15-18.xlsx')
  19. fiscal_calendar = pd.DataFrame([])
  20. #create new data frame with only two columns
  21. fiscal_calendar['InvoiceDate'] = calendar['Date']
  22. fiscal_calendar['Week'] = calendar['Week.1']
  23.  
  24.  
  25. #loop through csv files to format and save to new location
  26. for i in range(len(files)):
  27. #reads unformatted excel file into dataframe
  28. df = pd.read_excel(starting_folder+files[i])
  29. #change dtype of dates in report to date time
  30. df['InvoiceDate'] = pd.to_datetime(df['InvoiceDate'])
  31. #drop unwanted rows
  32. df = df.drop(df[df.Brand == 'NONPROD'].index)
  33. #add in fiscal week based on invoice date
  34. Sygma = pd.merge(df,fiscal_calendar, how = 'left', on = 'InvoiceDate')
  35. #save to csv for db loading
  36. Sygma.to_csv(save_folder+save_files[i],index = False)
Add Comment
Please, Sign In to add comment