Convert Groupby Result on Pandas Data Frame into a Data Frame using …. to_frame()

Lucas Jellema 6
0 0
Read Time:1 Minute, 13 Second

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:

image

Just applying the groupby with the grouper – in order to aggregate over each Quarter of data grouped by day of the week – gives me a Series object:image

However, by applying to_frame() I get the shiny new Data Frame I was after:

image

 

 

About Post Author

Lucas Jellema

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director and Oracle Developer Champion. Solution architect and developer on diverse areas including SQL, JavaScript, Kubernetes & Docker, Machine Learning, Java, SOA and microservices, events in various shapes and forms and many other things. Author of the Oracle Press book Oracle SOA Suite 12c Handbook. Frequent presenter on user groups and community events and conferences such as JavaOne, Oracle Code, CodeOne, NLJUG JFall and Oracle OpenWorld.
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

6 thoughts on “Convert Groupby Result on Pandas Data Frame into a Data Frame using …. to_frame()

  1. Thank you for the post, I have a question. How can I split the data after groupby in a way that each dataframe represents a group. For example, I have a dataframe with data from NYC, DC, CF and I want to turn it into 3 individual dataframes for each state

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Next Post

Introduction to Oracle Machine Learning - SQL Notebooks on top of Oracle Cloud Always Free Autonomous Data Warehouse

One of the relatively new features available with Oracle Autonomous Data Warehouse is Oracle Machine Learning Notebook. The description on Oracle’s tutorial site states: “An Oracle Machine Learning notebook is a web-based interface for data analysis, data discovery, and data visualization.” If you are familiar with Jupyter Notebooks (often Python […]
%d bloggers like this: