Hi. In this video we will learn more about the pandas apply method.
Apply method can be used on data frame and also a panda series.
And what does it do is to apply a function, uh,
to the elements of the series or data frame one by one,
basically like a four loop.
Let's see now the first example on a series. Uh,
here I applied a, uh, log method,
which basically calculates, uh,
a algorithm to each element of the second column.
Here I did the same using the Python s lambda function.
And here on the third example, I created my own function,
which gets us input a character and returns just Cisco Windows,
Microsoft or Domino string. If it is present in the whole string,
we can apply this function on the attack name column,
and this way we can create a new series and open this to data frame if you want.
Pan does apply. Method is very flexible, but is, uh, slow, uh,
because it applies your function element by element in the cre,
like in a for loop, basically, as I mentioned.
So the preferred way is to make calculations on a series or data frame in
a vectorized version. And if you not have the option,
uh, use the apply method or the for loop.
Now, when using the apply method on a data frame, the usage is the same,
but here another element is to consider, and that is the axis.
We can apply the function using apply method on each of the column or each of
the row. Uh, this is the axis parameter. Uh, let's explain it in the,
on an example here.
In the first and second case,
I applied the minimum and mean function
and apply does it by default on the rose axis.
That means it calculates minimum and the mean of each column.
These are aggregate functions. Uh,
so the result is just series with two values. As we see.
When we apply a function, which is not an aggregate function,
we get a data frame, we transform values
like this one.
We can also use another axis on which apply method applies its used function.
Here is an example
I created new series where each row contains string,
which contains minutes and seconds. So the data from two columns,
notice the axis parameter is set to columns.
The apply method is going now row by row and takes us one row,
like a panda series with two elements. So basically Panda thinks,
uh, this is a series, then applies this,
uh, function to the second series,
then to the third one and so on. And, uh, other axis,
uh, in these first examples goes this direction.
In our next video, we will learn more about how to merge data frames.