Hello everyone and welcome to the group by lecture for pandas in this lecture we're going to show you
how to use the group by method to light a group rows of data together and call aggregate functions.
Now before we actually jump into the Jupiter notebook Let me quickly explain what group buy is in case
you don't have sequel experience or you all experience or most people are familiar of what a GROUP BY
statement actually does.
To put it simply grouped by allows you to group together rows based off of a column and then perform
some sort of aggregate function on them.
For instance in this example here we have three partitions of IDS one two and three.
And then we have several values for them we can group by the ID column and aggregate them using some
sort of aggregate function in every function is basically just a fancy term for any function that takes
and many values and then spits out or outputs a single value such as taking the sum of a bunch of values
and outputting the results or taking the average or standard deviation etc..
These are all aggregate function and that's all a group by method does it allows you to choose a column
to Group By.
It gathers all those rows together based off that column value.
And then you perform some sort of aggregate function on it.
All right.
But that being said let's go ahead and learn how to use GROUP BY with pandas.
I'm going to go ahead and jump to the Jupiter notebook.
All right so here we are at the Jupiter notebook.
I went ahead and copied and pasted this line of data.
It's basically just a dictionary from the notebook and this note book is just called by one of the pandas
folder.
OK you can go ahead and type this up manually but I would just say go ahead and copy this data just
a dictionary and then you can go ahead and say PD data frame and then passen that data dictionary and
it should automatically then create a data frame that looks like this.
And basically this is just a made up data.
We have a company column with three company codes.
Google Microsoft and Facebook.
Now we have a person column with a bunch of unique people in it.
And then we have some sales numbers 200 120 etc..
Now what we can do is use that group by method to group rows together based off of a column name for
instance let's go ahead and group by the company I'm going to go on and say ZF and then you say group
by call as a method and you pass in the column you want to group by this case.
I want a group by the company column and notice what I get out here I just do this.
I will get a group by object and it just points to where it's stored in memory.
What I can do is store this as a new variable so I can say something like like come for my company and
I have this group by object.
Then the second step of using Group By.
After you've grouped by that column is to go ahead and call some sort of aggregate function on it.
So for example if I wanted to get the mean by company it would look at the sales column and give me
the mean or average sale by the company.
The reason it can't do this for the person column is because these are strings and there's no way you
can just take the mean of a string and that means panderers just automatically ignores anything that's
a non-numeric column.
Likewise we can go ahead and perform other aggregate functions such as some of everything as far as
sales per company.
You can even do standard deviations the Vestey the etc. and you should notice that what I get back is
an actual data frame with an index called Company and the column value of sales.
And that means I could do stuff such as index offer so I could say by come up let's go ahead and sum
up all the sales by company so that some of sales by company and then I could even call something like
Dot ellos C F B.
And that gives me the sales some of Facebook now a lot of times what you're going to be doing is not
actually setting this group by object as a variable.
You'll probably would just call this all in one line.
Once you start getting really comfortable with pantless they'll say something like group by company
then you call something like some.
And then if you just want it for Facebook you are the call FBI and that's all the same thing just in
one line now.
Some other useful aggregate functions that you may find yourself doing are things such as count which
just counts the number of instances or column.
In this case it was able to return the person column because it's able to count how many instances of
a person occur in that column or company.
So we have two people and they have two sales each and that's makes sense.
You can also do things such as max and min.
And note here that it also returns the person because Python is able to store things in alphabetical
order and that's basically what I'm trying to show here.
The max is the latest towards the end of the alphabet and if you call in that's the earliest in the
alphabet C and C we have agencies here.
However you probably shouldn't be using min or max with a string column.
You'll probably use all these angry functions really with numeric columns.
One last useful thing I want to show you with group by is a lot of times you can use GROUP BY with the
described method and that gives you a bunch of useful information all at once so you can say ZF thought
group by the company name and then say the scribe and it will give you a bunch of information.
So for instance to count the mean the standard deviation the men and the max as well as the core tile
values for each company.
And if you don't like this format and you can actually transpose this so you can say something like
that transpose if you prefer each company to be a column instead of an actual index.
So whatever format you like better you can describe to that and then you can actually just call column
names off of this.
If you're just interested in a single company or a single column.
All right that's really it for group by.
It's quite basic.
You just call the data frame say Dopp grouped by the column you want to group by and then whatever aggregate
method you want to aggregate the column values to a single value.
And it usually only works on numeric columns depending on what kind of aggregate function you're working
with.
You'll get a lot more practice of group by as we go through the exercises as well as the capsule projects
.
All right.
Thanks everyone and I'll see you at the next lecture