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?
df['col2'] = df['col1'].cumsum()[1,2,3,4,5], leading to an intuitive but wrong solution usingcumsum(). An example input like[3, 5, 2]would not have had this problem.