pandas: adding rows in a column

i have a dataframe like this,

Count
1
0
1
1
1

I want to add N and N+1 in count column and store it in N, is it possible to do in pandas way?

result should like this, technically it is cumulative sum:

Counts
1
1
2
3
4

Upvotes: 0

Views: 76

Answers (1)

Spinor8
Spinor8

Reputation: 1607

You can use the cumulative sum function, cumsum().

df = pd.DataFrame([1, 0, 1, 1,1], columns=['Count'])

df['Counts'] = df['Count'].cumsum()
print(df)

giving you the desired output.

   Count  Counts
0      1       1
1      0       1
2      1       2
3      1       3
4      1       4

Upvotes: 2

Related Questions