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)

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

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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With