I have a dataframe and I want to add a column that should be the accumulative sum of one of the columns but only if the value of another column is a specific one.
For example, my dataframe is as follows:
| Type | Quantity |
| A | 30 |
| B | 10 |
| B | 5 |
| A | 3 |
I would like to add a column SumA that would only do the accumulative sum of the quantities when Type == A.
I have tried this:
data['SumA'] = data['Quantity'].cumsum() if data[(data['Type'] == 'A')]
I keep getting errors and I'm not sure how I can solve them, could someone please give me a hand?
I would like to get something like this:
| Type | Quantity | Sum A | Sum B |
| A | 30 | 30 | 0 |
| B | 10 | 30 | 10 |
| B | 5 | 30 | 15 |
| A | 3 | 33 | 15 |