WEBVTT

00:01.590 --> 00:07.260
Welcome to our first coding video on group by operations in this and the next video.

00:07.260 --> 00:11.640
We want to figure out and really understand what group bite us in the background.

00:11.700 --> 00:15.410
And we also want to understand what a group buy up sectors.

00:15.420 --> 00:20.500
So first of all we will have a deeper look into this split part of split apply and combine.

00:20.730 --> 00:28.620
But first of all let's import pandas and we are working with the Titanic dataset and let's have a look

00:28.620 --> 00:29.820
here at the first five.

00:29.820 --> 00:36.500
Growth in the last five rows and also at some meta information with the Inform method

00:39.810 --> 00:42.950
and for the time being we want to work with a toy example.

00:42.960 --> 00:50.570
So we slice our Titanic data frame only for the first 10 rows and for the column sex and age.

00:50.580 --> 00:54.900
And we are saving here the slice in the variable Titanic slice.

00:54.900 --> 01:00.510
So let's do this and let's have a look here at our slice.

01:00.510 --> 01:03.700
So here we have the first 10 passengers.

01:03.720 --> 01:06.280
And the column sex and age.

01:06.540 --> 01:11.250
And now we want to use the group by method on our slice here.

01:11.250 --> 01:16.110
And what we actually want to do we want to split here our data frame by the sex column.

01:16.120 --> 01:18.450
So the key is the sex column.

01:18.570 --> 01:25.080
And by doing so we expect that we're splitting our data frame and then we get all rows of passengers

01:25.080 --> 01:29.190
who are female and we get all rows and passengers who are male.

01:30.060 --> 01:34.100
So let's also press shift tab within the brackets.

01:34.350 --> 01:40.650
And the most important parameter is here the by parameter where we have to define the keys by which

01:40.650 --> 01:43.050
we want to slice our data frame.

01:43.050 --> 01:46.370
And here we have to pass one or several columns.

01:46.380 --> 01:50.340
And in this case here we are passing the sex column.

01:50.820 --> 01:52.680
So let's do this here.

01:54.210 --> 02:00.380
And here we can see that we get something like a group by object so let's start here.

02:00.390 --> 02:08.840
The group by object in the variable GEO Group by object and let's also check the data type of GPO.

02:09.750 --> 02:18.070
So here it's a data frame grouped by object and same as this all data types are classes in Python.

02:18.110 --> 02:25.450
Also the group by object has specific attributes and methods and one attribute is the group's attributes.

02:25.450 --> 02:26.960
So let's try this out here.

02:28.760 --> 02:32.020
And here we can see that we have two groups and our group by object.

02:32.030 --> 02:36.700
So we have the group female with actually all rows where we have a female.

02:36.710 --> 02:39.990
So we have in the rows 1 2 3 8 9 female.

02:40.010 --> 02:42.760
So let's check this out in our data frame.

02:42.830 --> 02:53.950
1 2 3 8 and 9 so here we have actually the index labels where we have females in our data frame and

02:54.010 --> 02:56.190
the second group is the main a group.

02:56.980 --> 03:04.480
And also here we have the index labels father rows that we have made is that we have here 0 4 5 6 7

03:04.870 --> 03:13.450
0 4 5 6 7 and now we could also transform our group by object into a list by using here the list function

03:13.480 --> 03:22.310
and we save our list and the variable L and let's have a look here at L so it's actually at a first

03:22.310 --> 03:31.320
glance hard to see what we have here but apparently our list has two elements so the first element starts

03:31.320 --> 03:36.560
here with the bracket and ends here and the second element starts and ends here.

03:36.720 --> 03:41.970
So let's check first of all the length of our lists or the amount of elements.

03:41.970 --> 03:47.650
So we have two elements and now let's uh select the first element of our list.

03:49.270 --> 03:54.720
So here we have for the first element and here it looks like a table.

03:54.730 --> 04:00.130
So we have two brackets and apparently our total consists of two elements.

04:00.130 --> 04:01.830
So we have here the string female.

04:02.290 --> 04:05.920
And it seems like that we have here a data frame with all rows.

04:05.950 --> 04:06.970
We have a female.

04:07.900 --> 04:10.150
And we can also check here the data type.

04:10.690 --> 04:17.470
So it should be a total and now we can also select the first element in our total.

04:17.480 --> 04:18.990
So it should be a female.

04:19.550 --> 04:22.920
So let's try this out and it's a female.

04:23.420 --> 04:25.790
And this is apparently a string object.

04:25.940 --> 04:29.540
And let's also select the second element of our total here.

04:29.540 --> 04:31.360
So this looks like a data frame.

04:33.440 --> 04:39.740
And indeed here we have a data frame with all rows where we have females and still here on the left

04:39.740 --> 04:44.270
side the index labels are the same as here and our original data frame.

04:44.870 --> 04:49.900
So here we are for the females the index labels 1 2 3 8 and 9.

04:51.680 --> 04:54.350
And also here we have 1 2 3 8 and 9.

04:55.130 --> 04:56.990
And we can also check out the data types.

04:56.980 --> 05:01.090
So this should be a panda's data frame.

05:01.100 --> 05:05.980
Exactly and for the sake of completeness we can also check in.

05:06.010 --> 05:09.100
The second element and our total here.

05:09.100 --> 05:14.740
So this time we have for a total with the first element the string male and the second element.

05:14.770 --> 05:22.990
Our data frame with rows where we have male passengers.

05:23.050 --> 05:25.640
All right.

05:25.700 --> 05:27.900
So what the group by method actually did.

05:27.920 --> 05:34.550
It's it our original data frame into a two data frames and to the first data frame contains all rows

05:34.580 --> 05:36.030
with female passengers.

05:36.110 --> 05:43.280
And the second data frame contains all rows with the male passengers and alternatively we could also

05:43.280 --> 05:48.960
slice our data frame with the load operator and slice for all rows.

05:49.010 --> 05:50.830
The sex column is female.

05:50.870 --> 05:53.480
So we can also do this on display here.

05:53.810 --> 05:56.930
And here we can see that we get here the same slice.

05:58.040 --> 06:04.760
So here we have all rows with teammates and here we have all rows with females and we can also save

06:04.900 --> 06:08.590
the slice with all females in the variable Titanic slice female.

06:09.490 --> 06:11.920
And we can do the same with male

06:15.670 --> 06:21.910
and then we can check with the equals method whether the data frame was all female passengers which

06:21.910 --> 06:28.780
we created here with the log operator it's actually the same as our slice the group by object here.

06:29.740 --> 06:32.230
So here we get a true.

06:32.740 --> 06:39.610
So this confirms that the group by method splits our data frame by the key is sex and creates two data

06:39.610 --> 06:43.390
frames one female data frame and one male data frame.

06:43.780 --> 06:50.700
And we can also iterate over our group by object with a for loop and for each element in our group by

06:50.700 --> 06:58.050
object we want to print the data frame so we can also do this here and we get here our female data frame

06:58.080 --> 07:01.170
and our male data frame.

07:01.210 --> 07:01.680
All right.

07:01.690 --> 07:09.820
So now let's go also back to our slides here and this is actually exactly what we did right now.

07:10.720 --> 07:15.660
So we created here a group buy up checked and this is actually the split part.

07:15.940 --> 07:21.940
So we had to hear our original data frame or actually our slice of our data frame with nine passengers

07:21.970 --> 07:28.680
and the sex and age column and then we apply to the group by method by the key sex.

07:28.690 --> 07:35.500
And by doing so we split it our data frame into two data frames where in the first data frame we have

07:35.560 --> 07:43.570
all female rows here and in the second data frame we have here all the rows that we have a male passenger.

07:43.990 --> 07:46.060
So this is now our group by object.

07:46.060 --> 07:50.950
So we have two groups two data frames split by the key sex.

07:51.080 --> 07:51.500
All right.

07:51.510 --> 07:57.390
So we are finished now with this video and in the next video we can see that we can also split a data

07:57.390 --> 07:59.330
frame by more than one key.

07:59.340 --> 08:04.260
So by many keys by two or three or four keys so hope to see an error by.
