0

I'm currently trying to calculate the new DataFrame column. It should equal the sum from 0 to the value of the existing column. For example, I have such DataFrame df:

   col1  
0     1
1     2
2     3
3     4
4     5

And now, I want to calculate the second column as a sum from 0 to the first column value.

   col1  col2
0     1     1
1     2     3
2     3     6
3     4    10
4     5    15

I tried this code

df['col2'] = np.arange(0,df['col1']+1,1).sum()

But get the error

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Any suggestions?

4
  • 5
    df['col2'] = df['col1'].cumsum() Commented Dec 24, 2022 at 8:58
  • I need sum from 0 to the col2 value Commented Dec 24, 2022 at 9:40
  • As an aside, your question was unnecessarily difficult to understand because you used a degenerate example input [1,2,3,4,5], leading to an intuitive but wrong solution using cumsum(). An example input like [3, 5, 2] would not have had this problem. Commented Dec 24, 2022 at 10:03
  • @JohnZwinck I see, I misunderstood the question, bad example indeed Commented Dec 24, 2022 at 10:35

1 Answer 1

2

What you want is the sum of the first N positive integers. That has a closed form solution: N(N + 1)/2. The advantage of using such a solution is that you don't need to iterate over a sequence.

Using your example, the solution is:

df['col1'] * (df['col1'] + 1) // 2
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.