WEBVTT

00:01.020 --> 00:07.710
In this video we learn how to handle and deal with a mighty index that we create and group by operations.

00:07.710 --> 00:13.380
So when performing group eye operations in many cases we are getting a data frame with a mighty index

00:13.740 --> 00:16.300
also called hierarchical index.

00:16.380 --> 00:23.160
So let's have a look at the example and first of all we are importing pandas and we are working with

00:23.160 --> 00:31.440
the Titanic data set and we only import these survived PE class six eights and Fair column.

00:31.440 --> 00:39.920
So let's have a look at our data frame so here we have the five columns and now we want to group our

00:39.920 --> 00:43.250
data frame by the sixth column and peak last column.

00:43.460 --> 00:49.700
So we are actually creating six groups and for each group we actually want to calculate the survival

00:49.700 --> 00:50.520
rate.

00:50.540 --> 00:58.130
The average age and the average fan we can do this by applying the mean method in our group by object.

00:58.130 --> 01:03.680
So nothing new here and we are starting the residing data frame and the average summary.

01:03.890 --> 01:09.260
So let's do this and let's have a look at our new data frame summary.

01:12.300 --> 01:19.240
So here we can see we have the survival rate that the average age and the average fare for all six groups.

01:19.260 --> 01:25.020
So for example we have the group female passengers in the first class female passengers and the second

01:25.020 --> 01:31.350
class and so on and here on the left hand side we actually see a mighty index.

01:31.410 --> 01:38.640
So to say we've actually had two columns that serve s the index and we fear the outer index level.

01:38.670 --> 01:47.420
But to see the sex column and we have the index level PE class and our mighty index data frame is here

01:47.420 --> 01:50.050
first of all sorted by the Alta index.

01:50.040 --> 01:55.800
So first of all we have here the female passengers and then we have 50 male passengers.

01:55.800 --> 01:58.020
And within the groups female and male.

01:58.380 --> 02:05.010
So the OCR sorted by the peasants class so first second and third first second and third.

02:05.010 --> 02:11.340
So by creating your a group by object by more than one column and applying the mean method we are actually

02:11.340 --> 02:15.530
creating here a brand new data frame with a mighty index.

02:15.720 --> 02:22.950
So the outer index level is the sex column and the index level is the peak class column and we can also

02:22.950 --> 02:27.530
check the index type with the index attribute.

02:27.570 --> 02:35.550
So here we can see we have a mighty index and also with a mighty index we can perform label based indexing

02:35.850 --> 02:36.930
and we can do this here.

02:37.000 --> 02:42.000
At the local operator and we are opening here a scrap record.

02:42.050 --> 02:45.700
Now let's assume that we only want to select this row here.

02:45.810 --> 02:53.190
So females in the second class and we want to f your column so survived H and fare and we can do this

02:53.190 --> 02:56.850
by passing year on the right hand side of the comma.

02:57.070 --> 02:59.430
The column labels that we want to have here.

02:59.440 --> 03:07.170
So in this example all columns and on the left hand side of the comma we have to slice our mighty index

03:07.170 --> 03:12.670
and we can do this with the brackets here and again here we have a comma separator.

03:12.690 --> 03:19.140
And on the left hand side of the comma be defined which labels of our outer index we want to f.

03:19.140 --> 03:26.430
So your few methods and on the right hand side of the comma you can define which labels of our in the

03:26.460 --> 03:27.690
index we want to f.

03:27.720 --> 03:36.220
So you're a second class so let's do this a. getting actually all information off this row.

03:36.490 --> 03:44.020
So we have female and the second class and we have a survival rate of 92 percent and we have an average

03:44.050 --> 03:52.640
age of twenty eight years and we have an average ticket price of twenty one point nine and we can also

03:52.640 --> 03:54.020
select a single column.

03:54.020 --> 04:00.110
So instead of selecting all the columns that can pass here for example the column labeled H

04:03.380 --> 04:09.580
A.B. get to the Everett's h of all female peasants us in the second class or twenty eight point seven

04:09.590 --> 04:18.160
yes and with the slope level method we can actually swap here our index levels so we can swap our inner

04:18.160 --> 04:26.200
index PE class to the outer index level and we can swap our outer in Next Level Six to the next level.

04:26.530 --> 04:32.100
So let's try this out here and let's cross out here to sort in next so we'll do this later.

04:36.120 --> 04:42.240
So here we can see that now on the left hand side as the altar and next level we have peak class and

04:42.240 --> 04:48.750
on the right hand side as the inner index level we have sex and every time and we are creating a new

04:48.750 --> 04:49.120
model.

04:49.130 --> 04:54.000
And next that definitely makes sense to sort our index with the assault index method

04:57.360 --> 05:03.660
so here we can see we have our sorted mighty index and we have first of all the first class then the

05:03.660 --> 05:08.330
second class and the third class and within each class we have the order.

05:08.330 --> 05:09.980
Female and male.

05:10.680 --> 05:16.500
And if we are not happy with a mighty index we can also drop the mighty index and we can create an ordinary

05:16.500 --> 05:18.390
Range Index and we can do this.

05:18.390 --> 05:20.880
This the the reset index method

05:24.820 --> 05:29.040
so here we can see we dropped our mighty and next and we have a simple range.

05:29.080 --> 05:37.660
And next year starting from 0 to 5 and here we have sex and PE class as now ordinary columns of our

05:37.660 --> 05:38.830
new data frame here.

05:39.220 --> 05:44.320
So actually when we are performing group by operations in many cases we are creating a data frame with

05:44.320 --> 05:45.390
a mighty index.

05:45.670 --> 05:51.400
But if we are not happy with a mighty index we can simply drop the mighty index this.

05:51.460 --> 05:57.250
The reset index method and also here we can index and slice here for different values.

05:57.250 --> 05:58.940
So this is no problem.

05:58.960 --> 06:05.620
So there's actually no need to work with data frames with multi indexes but sometimes it makes our life

06:05.650 --> 06:13.390
easier and we should definitely know how to handle and work with the data frames that have multi indexes.

06:13.390 --> 06:17.680
All right so now we are finished with this video and I hope to see in the next video by.
