0

How to create cumulative sum (new_supply)in dataframe python from demand column from table

item   Date       supply  demand
A     2018-01-01  0       10
A     2018-01-02  0       15
A     2018-01-03  100     30
A     2018-01-04  0       10
A     2018-01-05  0       40
A     2018-01-06  50      50
A     2018-01-07  0       10
B     2018-01-01  0       20
B     2018-01-02  0       30
B     2018-01-03  20      60
B     2018-01-04  0       20
B     2018-01-05  100     10
B     2018-01-06  0       20
B     2018-01-07  0       30

New Desired table from the above table

item   Date       supply  demand  new_supply
A     2018-01-01  0       10      0
A     2018-01-02  0       15      0
A     2018-01-03  100     30      55
A     2018-01-04  0       10      0
A     2018-01-05  0       40      0
A     2018-01-06  50      50      100
A     2018-01-07  0       10      0
B     2018-01-01  0       20      0
B     2018-01-02  0       30      0
B     2018-01-03  20      60      110
B     2018-01-04  0       20      0
B     2018-01-05  100     10      140
B     2018-01-06  0       20      0
B     2018-01-07  0       30      0
2

2 Answers 2

0

You can change condition in boolean mask and change - to 0 in np.where:

m = df['supply'] != 0

df['new'] = np.where(m, df.groupby('item')['demand'].cumsum(), 0)
print (df)
   item        Date  supply  demand  new
0     A  2018-01-01       0      10    0
1     A  2018-01-02       0      15    0
2     A  2018-01-03     100      30   55
3     A  2018-01-04       0      10    0
4     A  2018-01-05       0      40    0
5     A  2018-01-06      50      50  155
6     A  2018-01-07       0      10    0
7     B  2018-01-01       0      20    0
8     B  2018-01-02       0      30    0
9     B  2018-01-03      20      60  110
10    B  2018-01-04       0      20    0
11    B  2018-01-05     100      10  140
12    B  2018-01-06       0      20    0
13    B  2018-01-07       0      30    0
Sign up to request clarification or add additional context in comments.

Comments

0

As the above data doesn't have any proper column which can be used to create groups, we need to create one:

df['grp_attr'] = df['supply'].clip(upper=1)
df['grp_attr'] = df[df['grp_attr'] != 0]['grp_attr'].cumsum()
df['grp_attr'] = df['grp_attr'].bfill().fillna(0).astype(int)

The df looks like this once you create grp_attr:

   item        Date  supply  demand  grp_attr
0     A  2018-01-01       0      10         1
1     A  2018-01-02       0      15         1
2     A  2018-01-03     100      30         1
3     A  2018-01-04       0      10         2
4     A  2018-01-05       0      40         2
5     A  2018-01-06      50      50         2
6     A  2018-01-07       0      10         3
7     B  2018-01-01       0      20         3
8     B  2018-01-02       0      30         3
9     B  2018-01-03      20      60         3
10    B  2018-01-04       0      20         4
11    B  2018-01-05     100      10         4
12    B  2018-01-06       0      20         0
13    B  2018-01-07       0      30         0`

Now we can groupby using grp_attr:

df['new_supply'] = df.groupby('grp_attr')['demand'].cumsum()
df.loc[df['supply'] == 0, 'new_supply'] = 0

Once you are done with your operations you can now drop the grp_attr columns from your dataframe.

df.drop(columns=['grp_attr'], inplace=True)

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.