WEBVTT - This file was automatically generated by VIMEO

0
00:00:07.740 --> 00:00:12.080
Hi. In this video we will learn more about the pandas apply method.

1
00:00:15.420 --> 00:00:19.090
Apply method can be used on data frame and also a panda series.

2
00:00:19.430 --> 00:00:23.050
And what does it do is to apply a function, uh,

3
00:00:23.150 --> 00:00:26.730
to the elements of the series or data frame one by one,

4
00:00:27.420 --> 00:00:28.830
basically like a four loop.

5
00:00:31.310 --> 00:00:34.700
Let's see now the first example on a series. Uh,

6
00:00:34.730 --> 00:00:38.260
here I applied a, uh, log method,

7
00:00:38.870 --> 00:00:41.140
which basically calculates, uh,

8
00:00:41.300 --> 00:00:44.920
a algorithm to each element of the second column.

9
00:00:49.920 --> 00:00:53.770
Here I did the same using the Python s lambda function.

10
00:00:58.800 --> 00:01:03.060
And here on the third example, I created my own function,

11
00:01:03.270 --> 00:01:07.700
which gets us input a character and returns just Cisco Windows,

12
00:01:08.380 --> 00:01:12.140
Microsoft or Domino string. If it is present in the whole string,

13
00:01:13.520 --> 00:01:16.220
we can apply this function on the attack name column,

14
00:01:16.480 --> 00:01:20.900
and this way we can create a new series and open this to data frame if you want.

15
00:01:22.290 --> 00:01:26.350
Pan does apply. Method is very flexible, but is, uh, slow, uh,

16
00:01:26.350 --> 00:01:30.070
because it applies your function element by element in the cre,

17
00:01:30.620 --> 00:01:33.070
like in a for loop, basically, as I mentioned.

18
00:01:35.100 --> 00:01:39.920
So the preferred way is to make calculations on a series or data frame in

19
00:01:40.000 --> 00:01:43.800
a vectorized version. And if you not have the option,

20
00:01:44.850 --> 00:01:46.830
uh, use the apply method or the for loop.

21
00:01:52.160 --> 00:01:56.820
Now, when using the apply method on a data frame, the usage is the same,

22
00:01:57.040 --> 00:02:01.620
but here another element is to consider, and that is the axis.

23
00:02:02.710 --> 00:02:07.650
We can apply the function using apply method on each of the column or each of

24
00:02:07.650 --> 00:02:12.360
the row. Uh, this is the axis parameter. Uh, let's explain it in the,

25
00:02:12.860 --> 00:02:16.490
on an example here.

26
00:02:16.550 --> 00:02:18.010
In the first and second case,

27
00:02:18.450 --> 00:02:22.450
I applied the minimum and mean function

28
00:02:23.460 --> 00:02:26.680
and apply does it by default on the rose axis.

29
00:02:26.750 --> 00:02:30.520
That means it calculates minimum and the mean of each column.

30
00:02:33.310 --> 00:02:35.780
These are aggregate functions. Uh,

31
00:02:35.800 --> 00:02:39.500
so the result is just series with two values. As we see.

32
00:02:42.700 --> 00:02:45.990
When we apply a function, which is not an aggregate function,

33
00:02:46.290 --> 00:02:48.670
we get a data frame, we transform values

34
00:02:52.490 --> 00:02:53.323
like this one.

35
00:02:59.050 --> 00:03:03.790
We can also use another axis on which apply method applies its used function.

36
00:03:04.880 --> 00:03:06.250
Here is an example

37
00:03:07.820 --> 00:03:11.780
I created new series where each row contains string,

38
00:03:11.950 --> 00:03:16.460
which contains minutes and seconds. So the data from two columns,

39
00:03:18.380 --> 00:03:21.040
notice the axis parameter is set to columns.

40
00:03:22.790 --> 00:03:27.770
The apply method is going now row by row and takes us one row,

41
00:03:27.960 --> 00:03:32.940
like a panda series with two elements. So basically Panda thinks,

42
00:03:33.040 --> 00:03:37.930
uh, this is a series, then applies this,

43
00:03:38.070 --> 00:03:39.970
uh, function to the second series,

44
00:03:43.070 --> 00:03:47.850
then to the third one and so on. And, uh, other axis,

45
00:03:48.030 --> 00:03:52.660
uh, in these first examples goes this direction.

46
00:03:57.680 --> 00:04:01.940
In our next video, we will learn more about how to merge data frames.
