python - Sum up previous values of a column -


i dealing rather simple problem, cannot find solution.

let's consider following table:

a

1

4

5

2

i create new column (b) sum of row until respective index:

a b

1 1

4 5

5 10

2 12

i thinking , searching solution not find one. have idea how proceed?

my approach:

  1. create default column 0. df['b']=0
  2. df['b']=df.b.shift()+df.a

is there way solve problem in python?

i'm making pretty big assumptions here because unfortunately have forced answering question that.

la = [1,4,5,2]  lb = [sum(l[0:i+1]) in range(len(l))] 

and if don't list comprehension here more verbose version of code:

lb = [] in range(len(l)):     lstuffabove = l[0:i+1]     lb.append(sum(lstuffabove)) 

also, check out, may of use: https://stackoverflow.com/help/how-to-ask


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -