Home > other >  cumsum bounded within a range(python, pandas)
cumsum bounded within a range(python, pandas)

Time:05-11

I have a df where I'd like to have the cumsum be bounded within a range of 0 to 6. Where sum over 6 will be rollover to 0. The adj_cumsum column is what I'm trying to get. I've search and found a couple of posts using loops, however, since mine is more straightforward, hence, is wondering whether there is a less complicated or updated approach.

 ---- ------- ------ ---------- ---------------- -------- ------------ 
|    | month | days | adj_days | adj_days_shift | cumsum | adj_cumsum |
 ---- ------- ------ ---------- ---------------- -------- ------------ 
|  0 | jan   |   31 |        3 |              0 |      0 |          0 |
|  1 | feb   |   28 |        0 |              3 |      3 |          3 |
|  2 | mar   |   31 |        3 |              0 |      3 |          3 |
|  3 | apr   |   30 |        2 |              3 |      6 |          6 |
|  4 | may   |   31 |        3 |              2 |      8 |          1 |
|  5 | jun   |   30 |        2 |              3 |     11 |          4 |
|  6 | jul   |   31 |        3 |              2 |     13 |          6 |
|  7 | aug   |   31 |        3 |              3 |     16 |          2 |
|  8 | sep   |   30 |        2 |              3 |     19 |          5 |
|  9 | oct   |   31 |        3 |              2 |     21 |          0 |
| 10 | nov   |   30 |        2 |              3 |     24 |          3 |
| 11 | dec   |   31 |        3 |              2 |     26 |          5 |
 ---- ------- ------ ---------- ---------------- -------- ------------ 
data = {"month": ['jan','feb','mar','apr',
                 'may','jun','jul','aug',
                 'sep','oct','nov','dec'], 
       "days": [31,28,31,30,31,30,31,31,30,31,30,31]}
df = pd.DataFrame(data)

df['adj_days'] = df['days'] - 28
df['adj_days_shift'] = df['adj_days'].shift(1)
df['cumsum'] = df.adj_days_shift.cumsum()
df.fillna(0, inplace=True)

Kindly advise

CodePudding user response:

What you are looking for is called a modulo operation.

Use df['adj_cumsum'] = df['cumsum'].mod(7).

CodePudding user response:

Intuition:

df["adj_cumsum"] = df["cumsum"].apply(lambda x:x%7)

Am I right?

  • Related