Python Forum

Full Version: summing rows/columns more quickly
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I recently had an assignment whereby we had to get sums of different columns. The original number of columns was 64 and we wanted to reduce it to 16 by summing up various columns. Thus, each row is a 1x64 array, essentially or you can think of it as an 8x8 matrix. To get the first 8 new sums, I simply took groups of 8, got their sums, and those were my first eight new columns/features. But, it seemed more difficult to get the next 8 sums. These might be considered the column sums if you're thinking of an 8x8 matrix. I ended up writing some pretty round-about code to get what I wanted but I was wondering if anyone had tips on how this code could be shortened to be more simplistic and quicker. Keeping in mind, I did this in pandas. My original code is below. Any tips or hints are much appreciated.

for index, row in df.iloc[:,0:64].iterrows():
    row=np.array(row)
    row=row.reshape(8,8)
    row_sum=row.sum(axis=0)
    row_sum=pd.DataFrame(row_sum.reshape(1,8),columns=[i for i in range(0,8)])
    column_sum=row.sum(axis=1)
    column_sum=pd.DataFrame(column_sum.reshape(1,8), columns=[i for i in range(8,16)])
    result = pd.concat([row_sum, column_sum], axis=1)
    df_2=df_2.append(result)
Jon
for index, row in df.iloc[:,0:64].iterrows():
    row=numpy.array(row)  
    row=row.reshape(8,8)
    row_sum=row.sum(axis=0)
    column_sum=row.sum(axis=1)
    concatenated = np.concatenate((row_sum,column_sum),axis=0)              #################
    result=pandas.DataFrame(concatenated.reshape(1,16), columns=[i for i in range(0,16)])#############
    df_2=df_2.append(result)
i concatenated row_sum & column_sum 1st, then called Dataframe() once... did this in python 2.7 just in case not working
Appreciate that! I only asked because I sent this code to my prof and he said it was overly complex and slow. Not sure what he had in mind or what other method he expected us to use. Writing it this way made sense to me as well from the standpoint of just being able to follow what the code is doing. I'll try and find out and if he explains it, I may post back.
nice,i'd like to see your next post