-1

I am trying to perform automated excel operations, but with cryptocurrency data from Binance. I converted the data to pandas df, and then into a NumPy array.

However, the thing is that I have 1 column of data which is volume. I want to perform a loop function that takes the latest data in my NumPy array and plusses/additions, the last value with the next value, and then iterates through the ids by one calculation at a time.

I seem to be stuck in trying to make a loop, that can iterate through my 1 column and make basic math operations on only a few of the rows at a time, but still being dynamic in that I don't have to type in the ids of the rows I want to make operations on each time.

Example:

   Volume
[0]   9212        
[1]   3021        [1]+[0] 3021+9212 = x
[2]   3201        
[3]   3921        [3]+[2] 3921+3201 = x
[4]   2010         
[5]   1999        [5]+[4]  1999+2010 = x

the idea is that it iterates by creating additions on the way up, or any other math operations.

Any suggestions as to what I can do?

d1sh4
  • 1,710
  • 5
  • 21

1 Answers1

0

For when your data is in a pandas DataFrame you can try preforming a rolling sum. like so:

df['Rolling_Volume'] = df['Volume'].rolling(2).sum()

This will create a new column called "Rolling_Volume" which will have the information you are looking for. You can read more here: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rolling.html

If you are insisting on preforming a rolling sum on a Numpy array it is explained here: Fast rolling-sum for list of data vectors (2d matrix)

Ofek Glick
  • 999
  • 2
  • 9
  • 20