Clean Pandas solution, 100% speed beaten
Intuition\nFirst find avarage price for each valid product. Then attach other products\' price.\n\n# Approach\nStep 1. Merge price with units\nStep 2. Filter by
# Intuition\nFirst find avarage price for each valid product. Then attach other products\' price.\n\n# Approach\nStep 1. Merge price with units\nStep 2. Filter by Date\nStep 3. Calcuate Revenue by Date\nStep 4. Find Average Price with GroupBy\nStep 5. Add other product\'s price\n\n# Result\n![image.png](https://assets.leetcode.com/users/images/81a40679-b8cf-4dcf-a173-f64b5d422daf_1695600836.538412.png)\n\n# Code\n```\nimport pandas as pd\n\ndef average_selling_price(prices: pd.DataFrame, units_sold: pd.DataFrame) -> pd.DataFrame:\n    df = pd.merge(prices, units_sold, on=\'product_id\', how=\'inner\')\n    df = (df\n            .query(\'purchase_date>=start_date and purchase_date<=end_date\')\n            .assign(revenue=df.price*df.units)\n            .groupby(\'product_id\', as_index=False)\n            .apply(lambda x: (x.revenue.sum()/x.units.sum()).round(2))\n            .rename({None:\'average_price\'},axis=1))\n\n    for pid in set(prices.product_id.unique()) - set(df.product_id.unique()):\n        df.loc[len(df.index)] = [pid, 0]\n        \n    return(df)\n```
['Pandas']