Pandas 1 line code solution
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nimport pandas as pd\n\ndef capital_gainloss(stocks: pd.DataFrame) -> pd.DataFrame:\n    return stocks.assign(price = stocks[\'price\'] * stocks[\'operation\'].apply(lambda x:-1 if x == \'Buy\' else 1)).groupby(\'stock_name\')[\'price\'].sum().rename(\'capital_gain_loss\').reset_index()\n\n```
['Pandas']