Pandas - simple and easy - Using diff() function
Intuition\n Describe your first thoughts on how to solve this problem. \n\ndiff() function takes diff of first and second row while groupby.\nRead about diff() 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\ndiff() function takes diff of first and second row while groupby.\nRead about [diff()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.core.groupby.DataFrameGroupBy.diff.html) (link)\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFor using diff() we have make sure the array is sorted\n1. sort\n2. diff\n3. remove nan in your df\n4. group by machine and take mean\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nimport pandas as pd\n\ndef get_average_time(activity: pd.DataFrame) -> pd.DataFrame:\n    activity = activity.sort_values([\'machine_id\', \'process_id\', \'timestamp\'])\n    activity[\'diff\'] = activity.groupby([\'machine_id\', \'process_id\'])[\'timestamp\'].diff()\n    activity = activity.dropna(subset=[\'diff\'])\n    return activity.groupby(\'machine_id\')[\'diff\'].mean().round(3).reset_index(name=\'processing_time\')\n```
['Pandas']