It is such a small thing. That you can look for in the docs, no Stackoverflow and in many blog articles. After I have used groupby on a Data Frame, instead of getting a Series result, I would like to turn the result into a new Data Frame [to continue my manipulation, querying, visualization etc.]. I feel at home in Data Frames and so do my tools and libraries. It took me a while – and a useful post on StackOverflow – to get things straight.
So here it goes. After my groupby, I use to_frame() to create a new Data Frame based on the result of the groupby operation. I use the parameter name to define the name for the column that holds the result of the aggregation (the mean value in this case)
# resample per quarter, (grouping by) for each weekday grouper = d.groupby([pd.Grouper(freq='1Q'), 'weekday']) # create a new data frame with for each Quarter the average daily death index for each day of the week (again, between 0.9 and 1.1) d2 = grouper['relative30DayCount'].mean().to_frame(name = 'mean').reset_index()
In pictures. First my original data frame:
However, by applying to_frame() I get the shiny new Data Frame I was after: