0

I would like tu sum the last X = 12 (in this case) values of a distribution given the following conditions:

  1. if there are less than 12 values it will do the cumulative sum of the values that exist.
  2. If there are more than 12 values it will sum the last 12.

I have been using the Numpy cumsum function to do cumulative sums, but this sums from the beginning to the end, y_position = np.cumsum(y_EBITDA) and I just want a cumulative of "X" number of values I determined.

Below I have an example of the input I have (EBITDA) and the output I need (Sum EBITDA 12M)

EBITDA  Sum EBITDA 12M
1       1
1       2
1       3
1       4
1       5
2       7
4       11
1       12
1       13
1       14
4       18
1       19
1       19
1       19
3       21
1       21
1       21
1       20
1       17
1       17
1       17
1       17
1       14
The Dan
  • 1,408
  • 6
  • 16
  • 41

2 Answers2

2

What you want is a rolling sum:

df['Sum EBITDA 12M'] = df['EBITDA'].rolling(12, min_periods=1).sum()
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
1

There are (at least) two ways to do that. One with rolling sum, and the other one with tail:

Method 1:

df.tail(12).sum()

Method 2:

df.rolling(12, min_periods=1).sum().iloc[-1]
Roy2012
  • 11,755
  • 2
  • 22
  • 35