I need to group the data by year
, place
and the interval of price
(step size is 5). For each group I want to estimate median level
df =
year place price level
1994 AAA 90 1
1993 BBB 89 1
1994 AAA 91 2
1998 AAA 92 3
1990 BBB 80 0
1994 AAA 90 1
1990 BBB 81 0
1991 BBB 92 1
I can group data and calculate the median values of level
, however I do not know how to add intervals of price
:
grouped_df = df.groupby(["year","place"]).agg({'level':'median'}).reset_index()
The correct grouped_df
should be structured as follows (the numbers might be different, it's just an example of the data structure):
grouped_df =
year place price_min price_max level
1990 AAA 80 85 1
...
UPDATE:
Final result should be something like this. So, basically price_min
and price_max
are lower and upper bounds, respectivelly:
year_ place_ level_median price_min price_max
0 1990 BBB 0 75 80
1 1991 BBB 1 80 85
2 1993 BBB 1 85 90
3 1994 AAA 1 85 90
4 1998 AAA 3 90 95