Home > Software design >  How can I get calculate buy and sell quantity in pandas?
How can I get calculate buy and sell quantity in pandas?

Time:11-04

picture

When I have data of quantity and buy and sell program, I want to get the cumulative quantity by adding a number when it is 'buy' and deducting a number when it is 'sell'. How can I do this in pandas? I know there is .cumsum() function to add it, but I don't know how to deduct it.

CodePudding user response:

You can flip the integer when selling:

df['quantity'].where(df['buy&sell'].eq('buy'), -df['quantity']).cumsum()
  • Related