WEBVTT

00:00.750 --> 00:07.380
In this video we learn how to apply the methods stick and unstick so whenever we have data frames with

00:07.440 --> 00:14.770
a mighty index stick and unstick are actually helpful tools to reorganize the data frame and reverse

00:14.790 --> 00:15.270
in a minute.

00:15.270 --> 00:22.030
What I mean here and first of all we import pandas and we are working with the some of them big state

00:22.060 --> 00:22.390
said

00:25.190 --> 00:30.950
so here we are for the first five rows of our data frame and now let's assume that we want to group

00:30.980 --> 00:33.920
our data frame by the columns country and medal.

00:34.430 --> 00:38.260
And for each group we want to count the amount of matters.

00:38.360 --> 00:44.390
By doing so we are creating a brand new data frame and we are signing the variable medals by country.

00:44.420 --> 00:49.700
So let's do this and let's have a look here at our data frame.

00:51.800 --> 00:52.460
So here we are.

00:52.460 --> 00:57.800
For each country the amount of gold Zuber and bronze medals.

00:57.800 --> 01:04.930
So for example for Algeria we have eight bronze medals five gold medals and two silver medals.

01:04.940 --> 01:10.050
So this is for all editions from eighteen hundred ninety six to 2012.

01:10.130 --> 01:17.060
And apparently here on the left hand side we have a mighty index and the outer index level is actually

01:17.060 --> 01:17.390
here.

01:17.400 --> 01:21.740
Country and the inner index level here we have the medals.

01:21.740 --> 01:29.010
Bronze silver and gold and doesn't see it are actually a pen serious because we only have one column.

01:29.220 --> 01:38.100
So this year the amount of medals and again here we can index or slice our pandas serious with the mighty

01:38.100 --> 01:45.040
index and we can do this with the local operator and we can perform later based indexing.

01:45.060 --> 01:52.290
So for example if you want to slice the outer index and we only want to select the United States so

01:52.290 --> 02:02.180
we can do this here we can see that we have over 1000 bronze medals over 2000 gold medals and one thousand

02:02.180 --> 02:08.870
two hundred seven medals and we've already learned how to simultaneously slice for the outer in next

02:08.870 --> 02:11.060
level and in the next level.

02:11.510 --> 02:18.470
So for example we could slice for the country United States and only the amount of gold medals we can

02:18.470 --> 02:19.860
do this.

02:20.090 --> 02:28.340
And actually here we should create the brackets so we have fewer United States for our outer index and

02:28.340 --> 02:36.960
then for our that index we want to select the gold medals.

02:36.970 --> 02:38.360
So now let's run this out.

02:39.310 --> 02:46.630
And here we only get the amount of gold medals that athletes from the United States won in the Summer

02:46.630 --> 02:47.530
Olympic Games.

02:47.530 --> 02:50.030
So two thousand two hundred thirty five.

02:50.200 --> 02:56.620
And we can also check the shape of our mighty and next panda serious matters by country with the shape

02:56.650 --> 02:58.380
attribute.

02:58.630 --> 03:04.380
So we FBA three hundred forty seven rows and apparently only a one column here.

03:04.690 --> 03:11.130
And now we can apply here the unstick method on our pandas serious medals by country.

03:11.500 --> 03:13.180
And let's simply have a look.

03:13.180 --> 03:14.100
What we get here

03:22.700 --> 03:29.240
and apparently we actually switched here our in that index level of matter and we actually created Tier

03:29.240 --> 03:32.920
3 new columns and created here a data frame.

03:32.930 --> 03:39.820
So now we have a data frame with the index country and we have three columns bronze gold and silver.

03:40.400 --> 03:46.270
So before bronze gold and silver were part of the inner index levels.

03:46.430 --> 03:51.380
And by applying the unstick method we switched the inner index level.

03:51.680 --> 03:54.040
And for each value in our in index level.

03:54.050 --> 03:56.130
So we have gold silver and bronze.

03:56.150 --> 03:58.040
We created a brand new column here.

03:58.040 --> 04:04.190
So we have the bronze column the gold column and the silver column and it's values and our new data

04:04.190 --> 04:07.100
frame here we have the amount of metals.

04:07.100 --> 04:14.670
So for example here for Afghanistan we have two bronze medals and by undertaking the index level we

04:14.670 --> 04:16.710
actually created here two new value.

04:16.710 --> 04:20.820
So Afghanistan gold medals and Afghanistan silver medals.

04:20.820 --> 04:26.970
And we have yet any and or missing values because here in our original and stick to pen the seriously

04:27.090 --> 04:33.540
mighty index we do not test for the group Afghanistan gold and Afghanistan silver and by UN sticking

04:33.540 --> 04:39.810
to the index level medals we're actually creating the groups Afghanistan gold and Afghanistan Silver

04:40.740 --> 04:41.510
and the same.

04:41.510 --> 04:43.530
We actually have here for all countries

04:47.060 --> 04:52.850
and we can also handle here the any end values but we will do this later about first of all let's go

04:52.850 --> 05:02.460
you're inside the brackets of the unstick method and that's press shift tab and this pyramid level.

05:02.460 --> 05:07.490
And here we can define which level off our mighty index we want to unstick.

05:07.530 --> 05:10.720
And by default to the inner index levels unstick.

05:10.740 --> 05:15.240
So here in this example the metal a column here the metal index

05:18.930 --> 05:25.400
so level equals minus 1 means we are unstuck in the index level at position minus 1.

05:25.410 --> 05:26.390
So an easy words.

05:26.400 --> 05:30.170
Actually the index level at the most the right hand side here.

05:31.080 --> 05:33.420
But uh we could also change this law.

05:33.420 --> 05:35.010
Let's do this here.

05:35.010 --> 05:37.620
So the default setting is minus 1.

05:38.580 --> 05:40.670
And by doing so we are sticking here.

05:40.680 --> 05:41.890
The medal and next.

05:42.000 --> 05:46.580
But we could also say that we want to unstick here the country index.

05:46.590 --> 05:51.940
And if we have a look at buff so the country indexes at index position minus 2.

05:52.050 --> 05:53.600
So medalists at minus 1.

05:53.600 --> 06:00.380
So the most right index and consequently country is at an exposition of minus two

06:05.240 --> 06:10.420
and now we are getting a different data frame and on the left hand side US index we have our medals

06:10.430 --> 06:17.360
bronze gold and silver and we actually transformed the country index as being the column C of our data

06:17.360 --> 06:17.750
frame.

06:17.750 --> 06:20.970
So far each country here we are getting a column.

06:20.970 --> 06:25.120
So for example the Afghanistan column or the Algeria column.

06:25.400 --> 06:28.070
So however this is here not really a good structure.

06:28.070 --> 06:31.270
So let's go back to a level a minus one here.

06:32.600 --> 06:40.480
So here we have our three columns bronze gold and silver and we still can see here the missing values

06:40.480 --> 06:43.720
for the groups that we created here with the unstuck method.

06:43.720 --> 06:49.450
So for example gold and silver medals for Afghanistan and to this another parameter here.

06:49.450 --> 06:57.190
So let's have a look here called full value and here we can define the value that should replace them

06:57.220 --> 06:58.640
and ends.

06:58.840 --> 07:01.150
And here in our example it should be zero.

07:01.150 --> 07:05.540
So there are no gold medals and no silver medals for Afghanistan.

07:05.650 --> 07:14.040
And I've already done this here so we are undertaking our medals by country pen that serious and we

07:14.040 --> 07:21.960
are undertaking it by the medals index or by the index and we are filling any and values by the value

07:21.950 --> 07:22.560
0.

07:22.890 --> 07:27.570
And then we are reassigning or overriding our variable medals by country.

07:27.600 --> 07:35.830
So let's do this here and let's have a look here at our brand new medals by country data frame.

07:35.830 --> 07:41.980
So yeah the first five rows and on the left hand side we have Afghanistan with two bronze medals zero

07:42.010 --> 07:44.980
gold medals and zero silver medals.

07:44.980 --> 07:48.550
And we can also check here the shape of our new data frame.

07:48.550 --> 07:54.190
So here we have one hundred forty seven rows centre three columns and this is actually one of the main

07:54.190 --> 07:56.740
benefits of the unstick method.

07:56.770 --> 08:02.300
So it changes the shape of our data frame our pandas serious from long format.

08:02.950 --> 08:09.310
So here we can see we have a long format that means we have many rows and only few columns so here we

08:09.310 --> 08:11.390
have 347 rows.

08:11.500 --> 08:15.660
So far each combination of country and medal we have a row.

08:16.090 --> 08:22.230
And by using the unstick method we are transforming the format from long to white slow.

08:22.230 --> 08:26.240
Now here we have a white format.

08:26.350 --> 08:31.870
So actually we have less rows and more columns compared to our long formats here

08:38.030 --> 08:42.830
and this is extremely useful in particular for data presentation purposes.

08:42.830 --> 08:49.150
So the right format is in many cases the more comfortable format with an improved readability.

08:49.160 --> 08:55.770
So this is quite here easy to read and understand and we can also rearrange here the order of our column.

08:55.770 --> 09:02.330
So first of all you want def code to then serve and then bronze and we can do this by selecting the

09:02.330 --> 09:08.500
columns gold silver and bronze in the order that we want to have and then be reassigned over right here

09:08.560 --> 09:11.080
our existing data frame medals by country.

09:11.150 --> 09:20.960
So let's do this and let's check again here the first five rows so yeah we have gold silver and bronze.

09:20.960 --> 09:28.400
And now we can sort our new data frame and we want to sort our new data frame by the code to serve and

09:28.400 --> 09:32.390
the bronze column and therefore we are passing here a list.

09:32.390 --> 09:38.930
Gold silver and bronze to the perimeter by and with the ascending power meter we can define the order

09:38.960 --> 09:45.150
for us three columns here and of course here for our purposes we want to have a descending order.

09:45.170 --> 09:52.300
So starting from the highest values and by passing year more than one column to the by power meter the

09:52.310 --> 09:54.770
sequences here actually are important.

09:54.770 --> 09:57.480
So the first hot hierarchy is actually gold.

09:57.540 --> 10:01.650
Then the second hierarchy serve and the third hierarchy bronze.

10:01.730 --> 10:07.820
So only if two countries suffer the same amount of gold medals then these countries are sorted by the

10:07.820 --> 10:09.440
silver column.

10:09.860 --> 10:16.040
And only if two or more countries have the same amount of gold and silver medals then these countries

10:16.040 --> 10:18.460
are sorted by the bronze medals.

10:18.590 --> 10:24.950
And last but not least if you want to override our data frame and safe our sorting we have to set the

10:24.960 --> 10:29.570
in-place perimeter to true.

10:29.580 --> 10:31.800
So now let's again check here.

10:31.800 --> 10:39.420
Our sorted data frame medals by country in the first 10 rows and no surprise we have here in the first

10:39.420 --> 10:44.430
row United States with the two thousand two hundred gold medals.

10:44.430 --> 10:53.040
Then we have the former Soviet Union and Great Britain Italy Germany and so on and now we can also visualise

10:53.070 --> 11:01.880
our results and therefore we import met plot lip as peyote and we are using the Seabourn style.

11:03.270 --> 11:07.920
And we actually want to create a pop plot for the ten most successful countries here.

11:07.950 --> 11:13.890
So we are plotting methods by country the first 10 rows and we are creating a bomb plot and we are using

11:13.890 --> 11:21.790
the fixed size twelve eight and as X Labor we have country X Y labor matters and the title is medals

11:21.810 --> 11:22.700
per country.

11:22.800 --> 11:24.960
So let's try this out here

11:31.010 --> 11:35.030
and here we have a bomb plot of the medals of the United States.

11:35.060 --> 11:38.100
So in blue we have the gold medals over 2000.

11:38.120 --> 11:43.370
Then we have the silver medals and green and the bronze medals and red.

11:43.610 --> 11:49.130
And we actually have this for the ten most successful countries here.

11:49.150 --> 11:55.510
All right so now last but not least I also want to introduce the step method so let's again have a look

11:55.510 --> 11:58.180
here at our data frame medals by country.

11:59.260 --> 12:00.660
So let's cross here out.

12:00.700 --> 12:02.860
And this is our data frame.

12:03.670 --> 12:08.950
And by using the stick method we are actually reversing our unstick method.

12:09.490 --> 12:16.360
So far with that the unstuck method we switched the inner index medal with gold silver bronze from being

12:16.360 --> 12:19.030
an inner index level to columns.

12:19.030 --> 12:23.520
And now with these test method we can recreate the inner index level medal.

12:23.530 --> 12:24.650
So let's do this year

12:32.100 --> 12:32.550
and again.

12:32.550 --> 12:34.320
Here we have our mighty index.

12:34.320 --> 12:39.870
So we have country s at the outer and next level and we have medals on the index level.

12:40.230 --> 12:42.910
And again you know we have a pen that serious.

12:42.990 --> 12:45.610
The values here the amount of medals.

12:45.870 --> 12:49.120
And now again we can unstick here our mighty index.

12:49.140 --> 12:50.400
So let's do this

12:53.800 --> 12:57.100
and again we are getting our data frame in the right format.

12:57.100 --> 13:02.640
So here we have the format in the index every better as the columns here of our new data frame.

13:02.650 --> 13:04.680
So go to the bronze.

13:04.690 --> 13:05.170
All right.

13:05.170 --> 13:11.290
So this was then an interaction on the unstick and the stick method and they are very helpful when we

13:11.290 --> 13:15.640
want to change the format of a data frame with a mighty index.

13:15.640 --> 13:22.290
And in particular love group by operations where we in many cases create multi indexes the methods stack

13:22.290 --> 13:26.700
and unstick very frequently used methods here.

13:26.700 --> 13:30.960
All right we are finished now with this video and I hope to see in the next one by.
