• 31 minutes ago
Turn rows of data into useful comparisons with Pandas groupby and aggregation. This hands-on Python tutorial uses an ice cream ratings dataset to show how grouping by base flavor reveals averages across numerical columns, and why counts add context to those results.

Learn to apply mean, count, min, max, and sum, then use agg() to run several calculations on selected columns. The lesson also demonstrates grouping by multiple columns, such as base flavor and whether a flavor was liked, and using describe() for a quick statistical overview.

A practical introduction to Pandas groupby, aggregate functions, and data analysis in Python—useful for beginners learning to summarize datasets, compare categories, and explore results in a Jupyter notebook.

Category

📚
Learning
Transcript
00:00Hello everybody. Today we're going to be taking a look at the groupby function and aggregating within pandas. Groupby is
00:06going to group together the values in a column and display them all on the same row. And this allows
00:11you to perform aggregate functions on those groupings. So let's start reading in our data and take a look. So
00:18we're going to do import pandas as pd. And then we're going to say our data frame is equal to,
00:24and we'll say pd.read underscore csv.
00:29We'll do an open parentheses, R, and our file path. And we're going to be looking at the flavors CSV
00:35right here. So right here, we have our flavor of ice cream. We have our base flavor, whether it was
00:41vanilla or chocolate, whether I liked it or not, the flavor rating, texture rating, and its overall or its total
00:48rating. Now, these are all my own personal scores. So, you know, I've spent years researching this. So these are
00:53all very accurate. But this should be a low-stress environment to learn group by and the aggregate functions.
00:59so the first thing that we can do is look at our group by now you can't group by well
01:04you can you can group by flavor but as you can see these are all unique values what we need
01:10is something that has duplicate values or or similar values on different rows that are grouped together so this base
01:17flavor is actually a perfect one to group it on and we'll do that by saying df dot group by
01:24do an open parenthesis and we'll just specify base
01:28flavor. And this will then group together those values. And I need to make sure I can spell properly. This
01:35will group those flavors together. So let's run this. And as you can see, it actually is its own object.
01:41So it has a group by data frame group by object. So now that we've grouped them, let's give it
01:47a variable. So we'll say group underscore by underscore frame. Let's say that's equal to. Let's copy this. We'll run
01:56it. And now,
01:58what we need to do is run our aggregations in order to get an output. So we're going to say
02:03dot mean, and that's all we're going to put just for now, just to get an output that we can
02:09take a look off and then we'll build from there. So let's go ahead and run this. And right here
02:15we have our base flavor, which is now saying is the index of chocolate or vanilla.
02:20and then it's taking the mean or the average of all the columns that have integers notice that it did
02:26not take the liked column and it did not take the flavor column because those are strings and they cannot
02:31aggregate those and we'll take a look at that later but it took all the values that have integers and
02:36then it gave us the average of those ratings
02:39Really quickly, I wanted to give a huge shout out to the sponsor of this entire Pandas series, and that
02:43is Udemy. Udemy has some of the best courses at the best prices, and it is no exception when it
02:48comes to Pandas courses. If you want to master Pandas, this is the course that I would recommend. It's going
02:52to teach you just about everything you need to know about Pandas. So huge shout out to Udemy for sponsoring
02:56this Pandas series, and let's get back to the video. So right off the bat, as averages, with chocolate, I
03:02have a much higher rating overall than the ones with vanilla bases.
03:06Now we can actually combine all of this together into one line, and we can do something like this. So
03:11we'll say df.groupBy, and we'll say dot mean, just like this. And this will actually run it. Before, we
03:21didn't have any aggregating function on there, so it didn't run. But now that we combine it all into one,
03:26it will run properly. Now there are a lot of different aggregate functions, but I'm going to show you some
03:31of the most popular ones and the most common ones that you will see. So let's copy this right here.
03:37So we can do dot count. And when we run this, we can look at the count. And this will
03:43show us the actual count of the rows that were aggregated. So for chocolate, we have three. So this can
03:48be three all the way across. And for vanilla, we had six. So we're looking at a higher count of
03:54vanilla, which if you're comparing it to this mean up here,
03:57That could be a big skew towards the chocolate because if you have one or two good chocolates, it could
04:03really pull the numbers up. Whereas if you had two good vanillas, but all the other ones were bad, it
04:07pulls that average down. So knowing the count of something is really good. Let's take a look at the next
04:14one. And we can do min and max. And I'll just run these really quickly. We can do min. And
04:19when we run this, the first thing that you should notice is that it now has a flavor and a
04:25liked column.
04:26And that's because min and max will actually look at the first letter in the string or the first set
04:30of letters if there are, you know, chocolate something. It'll look at the first and then it'll actually populate it.
04:36So chocolate with the CH chocolate is the very first or the minimum value for that string. And for a
04:44cake batter, that is the minimum value in vanilla as well. Now with the liked, it's interesting because apparently I
04:51liked all the chocolate ones. I'm gonna go take a look.
04:53So chocolate, I liked. Chocolate, I liked. Chocolate, I liked. So there is no no option in this liked column.
04:59So yes was the only option. And now let's look at max. And it should do the exact opposite, which
05:06is going to take the highest value, even if it's a string. So rocky road, the letter R comes later
05:11in the alphabet. So that's what it's looking at. And so does vanilla. And then we have yes as well.
05:16And then, of course, right here.
05:18it's taking the max value. So before when we were looking at min, I just focused on those, but it
05:23still does the exact same thing to these integer columns as well. So for the max value for vanilla, it
05:31was mint chocolate chip. That was our base. So I had a rating of 10 for this vanilla row or
05:36grouping. And then we can also look at the sum and there are all the sums for these. And again,
05:43it only does integer because we can't add the strings.
05:46Here are the sum or the total values for all of them. And for the total values, since we had,
05:51you know, six rows that were grouping into this vanilla, we now have a lot or a much higher score
05:57for vanilla. Now that's a really simple way to do your aggregations, but there is actually an aggregation function. And
06:04let's take a look at this because this is a little bit more complex. Although when I write it out
06:09or show you, hopefully it makes a lot of sense. We can do dot AGG.
06:14So this is our aggregate function, and what we need to pass into our aggregate function is actually a dictionary.
06:20So let's do an open parentheses, and we're going to do a squiggly bracket. And then we need to specify
06:26what we're going to be aggregating on, or what column. So let's do this flavor rating. Let's copy this. We'll
06:33do flavor rating, and I need to put that as a string. And then we'll do a colon.
06:38And now we can specify what aggregate functions we want. So we've done sum, count, mean, min and max, all
06:45of those. And we can actually put all of those into here and perform all of those aggregations on just
06:50one column. So let's make a list. And then let's say mean, max, count, and what's another one? Sum. Let's
07:03do all four of those only on this flavor rating column.
07:08And when we run this, we have our base flavor right here, chocolate and vanilla. But now we don't have
07:14multiple columns. We have one column with multiple columns of our aggregations. And it is possible to pass in multiple
07:22columns like that. So we'll do texture rating. And we'll just come right here and do a comma. Then we'll
07:30say texture rating. And then a colon. I don't know why I spelled it out when I...
07:38copied it, but I did. And then we'll do the exact same ones. And now when we run it, we're
07:44getting the exact same columns, mean, max, count, and sum for flavor rating, then mean, max, count, and sum for
07:50our texture rating. Now, so far, we've only grouped on one column, but we can actually group on multiple columns.
07:58Let's go back up here to our data. I should have just copied this down here. Let's go back down
08:03and just look at this. So really, we only grouped it on this base flavor.
08:09Well, you can do multiple groupings or group by multiple columns. So let's do our base flavor, which we did
08:15already, as well as the liked column. So we're going to say df.groupby, then we'll do an open parentheses.
08:24And then instead of just passing through one string, we're going to do a list and we'll say base flavor,
08:32oops, comma, and then we'll do liked. So now when it groups this,
08:39It should put two groupings. And let's run this and just see. Oops, I got to say, let's just do
08:44dot mean. So now we have our chocolate and a vanilla. And remember, chocolate only had yes. So that's the
08:53only one that it's going to group on. But vanilla had a no and a yes. So if we look
08:59at the vanilla, we have our base flavor vanilla. And then within liked, we have no and a yes.
09:05which can show us that within our vanilla, when we group on these, our nos were really low, but our
09:10yeses were really high. We actually had a pretty similar rating or very close to the same rating as the
09:16ones we really liked in chocolate. And just like we did above, we can take this .ag, and I'm going
09:22to copy this, and it'll perform it on each of those rows. Let me close that. And what did I
09:28do wrong? Oh, I need the squiggly bracket.
09:32And it'll show us each of those. So the mean, max, count, and sum for all of the chocolate and
09:38vanilla, as well as the groupings of liked, yes, and no. Now, after we've looked at all that, and that's
09:44how I usually do it, there is one shortcut function that can give you some of these things just really
09:50quickly. And so let's go back up here and take this. It's just called describe. And if you've ever done
09:57it, it's just going to give you some high level overview of some of those different aggregations.
10:02So let's run this. And it's going to give us our chocolate and vanilla. And within each column, it's going
10:08to give us our count, our mean, our standard deviation, I believe is what that is. Our minimum, 25%, 50,
10:1475, and 100, which is our max. Then our count and our mean. So a lot of those aggregate functions.
10:20But the describe is, you know, a very generalized function. We can't get as specific as we were with the
10:27previous ones that we were looking at.
10:29But I just wanted to throw this out there in case this is something that you'd be interested in because
10:33it, you know, technically is showing a lot of those aggregate functions just, you know, all at one time. So
10:39that is our group buying and aggregate functions within Pandas. I hope that that was helpful. I hope that you
10:43understood, you know, everything that we were working on. If you like this video, be sure to like and subscribe
10:48and check out all my other videos on Python as well as Pandas. I will see you in the next
10:52video.

Recommended