csv - python - replace last n columns with sum of all files -


i novice in python.

i have 8 csv files 26 columns , 600 rows in each. want take last 4 column of each csv files (column 22 column 25), read files , sum them replace 4 columns in each file. example (i showing random data here):

new-1.csv:

a   b   c   d   e   f   g   h     j   k 1   1   1   1   1   1   1   1   1   1   1 2   2   2   2   2   2   2   2   2   2   2 3   3   3   3   3   3   3   3   3   3   3 4   4   4   4   4   4   4   4   4   4   4 5   5   5   5   5   5   5   5   5   5   5 6   6   6   6   6   6   6   6   6   6   6 7   7   7   7   7   7   7   7   7   7   7 8   8   8   8   8   8   8   8   8   8   8 9   9   9   9   9   9   9   9   9   9   9  

new2.csv:

a   b   c   d   e   f   g   h     j   k 11  11  11  11  11  11  11  11  11  11  11 12  12  12  12  12  12  12  12  12  12  12 13  13  13  13  13  13  13  13  13  13  13 14  14  14  14  14  14  14  14  14  14  14 15  15  15  15  15  15  15  15  15  15  15 16  16  16  16  16  16  16  16  16  16  16 17  17  17  17  17  17  17  17  17  17  17 18  18  18  18  18  18  18  18  18  18  18 19  19  19  19  19  19  19  19  19  19  19 

now, want sum each element of "h, i, j, k" of these 2 files, replace files last 4 columns new sum.

modified new-1.csv:

a   b   c   d   e   f   g   h     j   k 1   1   1   1   1   1   1   12  12  12  12 2   2   2   2   2   2   2   14  14  14  14 3   3   3   3   3   3   3   16  16  16  16 4   4   4   4   4   4   4   18  18  18  18 5   5   5   5   5   5   5   20  20  20  20 6   6   6   6   6   6   6   22  22  22  22 7   7   7   7   7   7   7   24  24  24  24 8   8   8   8   8   8   8   26  26  26  26 9   9   9   9   9   9   9   28  28  28  28 

modified new-2.csv:

a   b   c   d   e   f   g   h     j   k 11  11  11  11  11  11  11  12  12  12  12 12  12  12  12  12  12  12  14  14  14  14 13  13  13  13  13  13  13  16  16  16  16 14  14  14  14  14  14  14  18  18  18  18 15  15  15  15  15  15  15  20  20  20  20 16  16  16  16  16  16  16  22  22  22  22 17  17  17  17  17  17  17  24  24  24  24 18  18  18  18  18  18  18  26  26  26  26 19  19  19  19  19  19  19  28  28  28  28 

i assuming should use panda or numpy this, not sure how it. suggestions/hints appreciated.

you can using numpy.

import numpy np  # list of files  file_list = ['foo.csv','bar.csv','baz.csv'] # 8 files  col_names = ['a','b','c','d','e','f'] # names till z if necessary first row, else skip  # initializing numpy array, containing sum last 4 columns  add_cols = np.zeros((600,4))  # iterating on .csv files  file in file_list :      # skiprows skip first row , usecols values in last 4 cols      temp = np.loadtxt(file, skiprows=1, delimiter=',' , usecols = (22,23,24,25) )     add_cols = np.add(temp,add_cols)  # again overwriting files, substituting last 4 columns sum     file in file_list :      #loading content file in temp      temp = np.loadtxt(file, skiprows=1, delimiter=',')     temp[:,[22,23,24,25]] = add_cols       # writing column names first      open(file,'w') p:         p.write(','.join(col_names)+'\n')      # appending final values in temp file csv      open(file,'a')  p:           np.savetxt(p,temp,delimiter=",",fmt="%i")  

now if file not comma separated , rather space separated, remove delimiter option functions delimiter taken space default. join first column accordingly.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -