Pandas oneliner, groupby with transform, no merge needed
Intuition\nWe simply have to find minimum year for each product, which you could do with transform - it is equivallent of window functions in SQL or pyspark.\n\
# Intuition\nWe simply have to find minimum year for each product, which you could do with transform - it is equivallent of window functions in SQL or pyspark.\n\n# Approach\n1. Create new column with minium year for each product:\n`sales.assign(first_year = sales.groupby(\'product_id\').year.transform(min))`\n2. Select only rows, where created first_year == year:\n`.query("first_year == year")`\n3. And finally select only relevant columns:\n`.iloc[:,[1,5,3,4]]`\n\n- Notice, that we dont have to use second dataframe at all, since all required columns are from `sales`\n\n**CAUTION**: however `.iloc[]` passes all testcases, it is always best practice to select by actual column names, becouse in case of merging empty dataframes, order of columns may change.\n`[[\'product_id\',\'first_year\',\'quantity\',\'product_name\']]`\n\n# Code\n```\nimport pandas as pd\n\ndef sales_analysis(sales: pd.DataFrame, product: pd.DataFrame) -> pd.DataFrame:\n  return sales.assign(first_year = sales.groupby(\'product_id\').year.transform(min)).query("first_year == year").iloc[:,[1,5,3,4]]\n```
['Pandas']