0

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
6
  • 4
    Are you trying to do a cumulative sum? Look into cumsum(). Commented Jul 6, 2018 at 9:17
  • thank you @Spinor8 Commented Jul 6, 2018 at 9:20
  • Could you be more clear in what you want to achieve?, how do you want your final dataframe to look like? Commented Jul 6, 2018 at 9:37
  • @Learner edited the question, Commented Jul 6, 2018 at 9:40
  • @Spinor8 please write your answer so that i can mark it. Commented Jul 6, 2018 at 9:40

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.