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:
- create default column 0. df['b']=0
- 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
Post a Comment