pandas || 2 lines, rank, merge || T/S: 92% / 99%
\nimport pandas as pd\n\ndef sales_analysis(sales: pd.DataFrame, product: pd.DataFrame) -> pd.DataFrame:\n\n    sales[\'rnk\'] = sales.groupby([\'product_id\'])
```\nimport pandas as pd\n\ndef sales_analysis(sales: pd.DataFrame, product: pd.DataFrame) -> pd.DataFrame:\n\n    sales[\'rnk\'] = sales.groupby([\'product_id\'])[\'year\'\n                       ].rank(method=\'dense\')\n                       \n    return sales.loc[sales.rnk == 1\n               ].merge(product).iloc[:,[1,2,3,4]\n               ].rename(columns = {\'year\': \'first_year\'})\n```\n[https://leetcode.com/problems/product-sales-analysis-iii/submissions/1088479316/](https://leetcode.com/problems/product-sales-analysis-iii/submissions/1088479316/)\n\n\n\nI could be wrong, but I think that time complexity is *O*(*N* log *N*)  and space complexity is *O*(*N*), in which *N* ~ `len(sales)`.
['Pandas']