IN TWO LINES | Runtime 544 ms Beats 98.39% of users with Pandas
\n# APPROACH\n\n\n\n# CODE\n\nimport pandas as pd\n\ndef capital_gainloss(stocks: pd.DataFrame) -> pd.DataFrame:\n    stocks.price = stocks.price.mask(stocks.op
\n# APPROACH\n\n![image.png](https://assets.leetcode.com/users/images/c105a8cd-9f8c-4c10-a160-10a1dfa1fee5_1713619342.3220844.png)\n\n# CODE\n```\nimport pandas as pd\n\ndef capital_gainloss(stocks: pd.DataFrame) -> pd.DataFrame:\n    stocks.price = stocks.price.mask(stocks.operation == "Buy", lambda x : -x)\n    return stocks.groupby(["stock_name"]).price.sum().reset_index()[["stock_name", "price"]].rename(columns={"price":"capital_gain_loss"})\n```
['Database', 'Python3', 'Pandas']