Posts

Showing posts from July, 2017

Batch rename and xls to csv

''' #Remove spaces from filename import os path = 'C:\\Users\\gaura\\Downloads\\estmst' #os.getcwd() filenames = os.listdir() for filename in filenames: os.rename(os.path.join(path, filename), os.path.join(path, filename.replace(' ', '-'))) ''' import os , pandas as pd path = 'C: \\ Users \\ gaura \\ Downloads \\ estmst' for filename in os . listdir(path): data_xls = pd . read_excel(filename, 'Main Dashboard Excel' , index_col = None ) data_xls . to_csv(filename + '.csv' , encoding = 'utf-8' , index = False )

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 )