Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regarding getting the rowsum of a python data frame

I have following python data frame

data={'1':[1,1,1,1],'2':[1,1,1,1],'3':[1,1,1,1]}
df=pd.DataFrame(data)

enter image description here

I need to get the sum of the rows in a such away that my final output should be like this,

enter image description here

So in this desired output, the second column should contain the row sum up to second column of the original data frame. So on.

To get this output, I wrote the following code,

sum_mat=np.zeros(shape=(3,3))

numOfIteration=3
itr=list(range(0,numOfIteration))

for i in range(0,3):
    for j in range(0,3):
        while i <= itr[i]:
            sum_mat[i,j]+= df.iloc[i,j]

print (sum_mat)

I am not getting an output here because the code is running forever (may be an infinite loop).

Can anyone suggest anything to get the desired output ?

May be there is more effective and easier way to do the same thing.

Thank you

UPDATE: i update the for loop as follows,

for i in range(0,3):
   for j in range(0,3):
        while i <= itr[i]:
           sum_mat[i,j] = df.iloc[:,0:i].sum(axis=1)

but it gives following error,

sum_mat[i,j] = df.iloc[:,0:i].sum(axis=1)
ValueError: setting an array element with a sequence.
like image 450
student_R123 Avatar asked Dec 01 '25 02:12

student_R123


1 Answers

this could work also

for i,row in df.iterrows(): #go through each row
    df.loc[i]=df.loc[i].cumsum() #assign each row as the cumulative sum of the row

output:

>>> df
   1  2  3
0  1  2  3
1  1  2  3
2  1  2  3
3  1  2  3

EDIT

can just do :

df=df.cumsum(axis=1)
like image 132
Derek Eden Avatar answered Dec 04 '25 11:12

Derek Eden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!