'pandas DataFrame groupby/freq with value range
Is there any method in python pandas to slice price range a DataFrame, not only time range, like groupby('D')? I need price range from this DataFrame, when price range is 5, and I want to other columns start/end/min/max of prices.
Time,Price,Volume
2022-04-10 00:01:00,100,5
2022-04-10 00:02:00,102,5
2022-04-10 00:03:00,104,5
2022-04-10 00:04:00,102,5
2022-04-10 00:05:00,105,5
2022-04-10 00:06:00,105,5
2022-04-10 00:07:00,106,5
2022-04-10 00:08:00,104,5
2022-04-10 00:09:00,103,5
2022-04-10 00:10:00,106,5
2022-04-10 00:11:00,108,5
2022-04-10 00:12:00,107,5
2022-04-10 00:13:00,105,5
2022-04-10 00:14:00,108,5
2022-04-10 00:15:00,104,5
2022-04-10 00:16:00,103,5
I'd like to get this:
Time,Start_price,End_price,Min_price,Max_price,Sum_volume
2022-04-10 00:01:00,100,105,100,105,25
2022-04-10 00:06:00,105,108,103,108,30
2022-04-10 00:12:00,107,103,103,108,25
Solution 1:[1]
Try this for making groupby col:
df['Price'] = df['Price'].astype(int)
r = df['Price'].iloc[0]
out = []
g = 0
for price in df['Price']:
if abs(price-r)>=5:
out.append(g)
g+=1
r = price
else:
if price < r:
r = price
out.append(g)
Output:
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2]
The code iterates on the Price column and creates groups based on the absolute difference of the observed max and mins.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
