Pivot and groupby
Intuition\nPivot organizes the table with end and start columns which makes it easy to create the processing_time column. \n\n\n# Code\n\nimport pandas as pd\n\
# Intuition\nPivot organizes the table with end and start columns which makes it easy to create the processing_time column. \n\n\n# Code\n```\nimport pandas as pd\n\ndef get_average_time(activity: pd.DataFrame) -> pd.DataFrame:\n    pivoted = activity.pivot(index=["machine_id", "process_id"], columns=\'activity_type\', values=\'timestamp\')\n    pivoted[\'processing_time\'] = pivoted[\'end\'] - pivoted[\'start\']\n    return pivoted.groupby(\'machine_id\').processing_time.mean().round(3).reset_index()\n\n\n```\n\nChaining version\n```\nimport pandas as pd\n\ndef get_average_time(activity: pd.DataFrame) -> pd.DataFrame:\n    return activity.pivot(index=["machine_id", "process_id"], columns=\'activity_type\', values=\'timestamp\')\\\n            .groupby("machine_id")\\\n            .apply(lambda x: (x[\'end\'] - x[\'start\']).mean().round(3))\\\n            .rename("processing_time")\\\n            .reset_index()\n```
['Pandas']