Pandas: Group by time and get the difference of the max and min values in each group

Photo by Markus Winkler on Unsplash

Here is an example of grouping in 3 min bins and get the difference of max and min values in each group.

Code

Input

                 Time  Price
0 2020-01-01 00:00:00 5
1 2020-01-01 00:01:00 9
2 2020-01-01 00:02:00 5
3 2020-01-01 00:03:00 2
4 2020-01-01 00:04:00 4
5 2020-01-01 00:05:00 4
6 2020-01-01 00:06:00 7
7 2020-01-01 00:07:00 3
8 2020-01-01 00:08:00 2

Output

                 Time  Price Diff
0 2020-01-01 00:00:00 4
1 2020-01-01 00:03:00 2
2 2020-01-01 00:06:00 5

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store