Pandas Beats 100%
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\n# Code\n\nimport pa
# 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\n# Code\n```\nimport pandas as pd\n\ndef average_selling_price(prices: pd.DataFrame, units_sold: pd.DataFrame) -> pd.DataFrame:\n    units_sold = units_sold.merge(prices, on=\'product_id\', how=\'left\')\n    units_sold[\'matching\'] = (units_sold[\'purchase_date\'] >= units_sold[\'start_date\']) & (units_sold[\'purchase_date\'] <= units_sold[\'end_date\'])\n    units_sold = units_sold[units_sold[\'matching\']==True]\n    units_sold[\'total_spent\'] = units_sold[\'units\']*units_sold[\'price\']\n    units_sold = units_sold.groupby(\'product_id\').agg(total_spent=(\'total_spent\', lambda x: sum(x)), total_units=(\'units\', lambda x: sum(x))).reset_index()\n    units_sold[\'average_price\'] = round(units_sold[\'total_spent\']/units_sold[\'total_units\'],2)\n    prices = pd.DataFrame(columns=[\'product_id\'], data=prices[\'product_id\'].unique())\n    return prices.merge(units_sold[[\'product_id\', \'average_price\']], on=\'product_id\', how=\'left\').fillna(0)\n```
['Pandas']