Hi.
In this video we will learn more about how to summarize data using
group by.
There are a few ways on how we can use the group by in pandas. Uh,
the most common usage is to use it together with some aggregate function.
Like in the first example here,
we calculated the average duration of attacks per protocol and then
sorted the result descending.
But group by can be used also with other functions, not just aggregates,
like mean minimum, maximum median or some for example,
here in the second example I wanted to see what are the top
three attacks, uh, with longest duration for each attack category.
We see here the attack category index of the top three rows and their value.
We can confirm this, uh,
with calculating the maximum for each of the attack category.
Other example, very similar to the last one. Maybe this,
uh, let's get top three attacks per each of the attack category, uh,
with longest duration. But we would like to see the whole row.
We can use the apply method for this. Here.
We basically applied the sort value method for each of the group and with head
method we took just first three rows.
The next example is, in my experience,
the most common usage of group by together with aggregate.
It is very flexible because we can nicely specify the columns,
which we want to calculate and it's aggregate statistics. This way.
I like it more because it is very similar,
like SQL Works using Group I instead of using, uh, pivot table
here in the next cell is the similar example as before. We just extended,
uh, the calculation for service and its count
here in this cell is an example on how to flatten the columns because they were
a multi index to have just one level to also preserve the aggregate information
in the column names.
In our next video,
we will learn more about how to style pandas data frames in Jupyter.