BossRoyce
BossRoyce

Reputation: 211

Cumulatively Reduce Values in Column by Results of Another Column

I am dealing with a dataset that shows duplicate stock per part and location. Orders from multiple customers are coming in and the stock was just added via a vlookup. I need help writing some sort of looping function in python that cumulatively decreases the stock quantity by the order quantity.

Currently data looks like this:

    SKU     Plant   Order  Stock
0   5455    989     2      90
1   5455    989     15     90
2   5455    990     10     80
3   5455    990     20     80

I want to accomplish this:

    SKU     Plant   Order   Stock
0   5455    989     2       88
1   5455    989     15      73
2   5455    990     10      70
3   5455    990     20      50

Upvotes: 0

Views: 25

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150785

Try:

df.Stock -= df.groupby(['SKU','Plant'])['Order'].cumsum()

Upvotes: 3

Related Questions