WEBVTT

00:01.280 --> 00:07.400
In this video we learn how to sort pen out serious so let's assume we have the H column of our Titanic

00:07.400 --> 00:14.570
data set and we want to sort the age from young to old or from 0 to young and we can do this with the

00:14.660 --> 00:22.990
sort values method so that's option number one we can sort a panda series by values but we can also

00:22.990 --> 00:26.590
swap a panda serious by the index.

00:26.590 --> 00:33.850
So let's go into our examples here and we import pandas and then we create a dictionary with the daily

00:33.850 --> 00:35.920
sales that we have on Monday.

00:35.920 --> 00:38.890
So one sense for Monday we have 10 sets.

00:39.190 --> 00:43.780
Then we have Wednesday 25 cents and then we have Tuesday success.

00:43.810 --> 00:46.590
So our dictionary has not really sorted.

00:46.620 --> 00:52.470
So Wednesday comes before Tuesday then we have a first day 36 sales.

00:52.480 --> 01:01.300
Then Friday to say is Saturday cereals layers because we do not work on Saturday and Sunday as we do

01:01.300 --> 01:02.920
not work on Sundays as well.

01:02.920 --> 01:08.920
We have also cereal sales but instead of having a zero we have just a month so we can do this and let's

01:08.920 --> 01:11.120
see what we get here.

01:11.470 --> 01:18.740
We get here and dictionary with um weekdays we'll use Paris and you can see here on Sunday it's still

01:18.820 --> 01:29.070
the value none included so let's pass our dictionary to PD dot series and let's create a pan that serious.

01:29.170 --> 01:35.230
So now you can see here our panel serious with on the left side our keys of our dictionary and in the

01:35.230 --> 01:36.800
same saw that as we passed it.

01:36.820 --> 01:43.210
So beginning with the day one and then they three they four five six seven.

01:43.210 --> 01:44.460
So here again.

01:44.800 --> 01:53.410
And we also have on the right side the values and you can see on Sunday we AFE and they end on missing

01:53.410 --> 01:53.970
value.

01:54.700 --> 02:00.340
So the key about num actually transforms and append us serious and the PAN US data frame to a missing

02:00.340 --> 02:07.750
value and and and as we do not have here the right sequence in our index so starting from Monday but

02:07.810 --> 02:15.940
then having Wednesday and then Tuesday we can sort our index with the site index method.

02:15.940 --> 02:23.260
So now you can see we have our index sorted starting from 1 and then we have 2 3 4 5 6 and 7 and we

02:23.260 --> 02:27.650
can also determine whether we want an ascending or descending order.

02:28.480 --> 02:32.920
So by default here we have an ascending order starting with them the lowest index.

02:33.400 --> 02:41.110
So here 1 so here is the salt index method we have a parameter ascending so let's press shift tap here

02:41.110 --> 02:49.340
and have a look so here we have the ascending parameter and by default it is set to true.

02:49.510 --> 02:50.670
So let's run again here.

02:51.460 --> 02:57.760
So we have here the ascending order but we can change this to the descending order by setting the parameter

02:57.760 --> 03:00.710
to force.

03:00.740 --> 03:06.800
So now we have from the descending order starting from Sunday or from seven seven six five four three

03:06.800 --> 03:08.000
two one.

03:08.000 --> 03:08.360
All right.

03:08.360 --> 03:15.010
So now let's change it back to ascending order.

03:15.010 --> 03:15.380
All right.

03:15.380 --> 03:17.900
So now we have changed to our that serious.

03:17.990 --> 03:23.810
Say it's by sorting the index and let's print again our panel series.

03:23.810 --> 03:29.300
And now you can see here that our change so sorting the index is not stored in memory so we haven't

03:29.300 --> 03:36.840
actually changed our series here and what we did here actually we just created a copy of our pandas

03:36.840 --> 03:42.030
series says and we uh sorted the index of the copy only.

03:42.210 --> 03:43.600
But uh yeah what.

03:43.680 --> 03:48.100
We want to change our original series and that's a solution for it.

03:48.900 --> 03:53.150
So let's go inside this site and next method and check with Chef tab.

03:53.160 --> 04:00.760
And here is the parameter in place and in place is by a defined set of files.

04:00.770 --> 04:04.540
And if it's true it performs the operation in place.

04:05.030 --> 04:10.460
So what in place is doing if it's set to fires all changes we made.

04:10.490 --> 04:18.290
So for example sorting the index are performed only on a copy and the original serious has not changed.

04:18.470 --> 04:25.190
And the fifth set of the in-place parameter to true then all changes also apply for the original series

04:25.190 --> 04:33.030
so the changes in place and the in-place perimeter and pan US is a very important parameter and you

04:33.030 --> 04:40.860
can see it in many methods for panda serious and also for panda data frames and the tabs you a lot to

04:40.860 --> 04:49.410
understand if a change is actually made on a copy or on the origin panda series or data frame and depending

04:49.410 --> 04:54.660
on what you actually want to do you can set the in place to true or false.

04:55.170 --> 05:02.220
So let's explicitly here introduce the in-place parameter and by default is set of false

05:04.980 --> 05:08.770
so now here we created a copy of states and decided to index.

05:08.790 --> 05:14.330
And if we print our original sales it's still not in place of the change here.

05:14.340 --> 05:17.580
But if we change the in-place parameter to true

05:21.490 --> 05:28.490
and now you can see here we do not see any uh printed copy of the say so that's the first good indication

05:28.490 --> 05:32.990
that we changed the original say it's the panda series and let's print here.

05:33.260 --> 05:35.250
Oh pen serious.

05:35.360 --> 05:36.680
And now we can see that.

05:36.860 --> 05:37.120
Yeah.

05:37.130 --> 05:38.390
That changes in place.

05:38.380 --> 05:44.240
So here we saw that our index and we changed our original pen series say it's all right.

05:44.240 --> 05:46.760
So now let's head over to a sorting value.

05:46.760 --> 05:54.560
So so far we only saw that our pandas serious by the index and we can also sort our serious by the values

05:55.280 --> 05:57.970
and we set in place to force.

05:58.260 --> 06:03.690
And let's see what we get here so by default the place apparently does set to fall.

06:03.710 --> 06:08.780
So I only want to make this explicitly here and we can see here on the right side.

06:08.820 --> 06:13.280
We have sorted ourselves pandas serious by the values.

06:13.450 --> 06:15.230
And then ascending order.

06:15.270 --> 06:26.000
So starting from zero Zs on Saturday and ending with 36 sets on Thursday and the end value is at the

06:26.000 --> 06:28.760
very end and also for the site various method.

06:28.760 --> 06:32.240
There are some parameters so we have the ascending parameter.

06:32.300 --> 06:40.090
We can determine whether we want to sort an ascending or descending manner so by default that the ascending

06:40.090 --> 06:41.490
parameter is set to true.

06:41.520 --> 06:43.280
But we can set it to false.

06:43.750 --> 06:45.130
Let's check here.

06:46.630 --> 06:54.110
So now we sort out our values in descending order starting with so the highest value 36 on Thursday.

06:54.340 --> 07:00.550
And we can also determine here you can see that still our any end value is in the last position and

07:00.550 --> 07:04.300
we can also determine the position of um and a values.

07:04.690 --> 07:12.490
So here we have last but we can also change this for example to first.

07:12.500 --> 07:17.350
So now here we have our end end value at the first position but in the end.

07:17.360 --> 07:20.120
So it makes more sense to have it at the last position

07:24.290 --> 07:30.430
and sorting ourselves from high to low makes makes sense so we keep ascending equal scores.

07:30.680 --> 07:34.880
And once we are happy here we can set our in-place parameter to true

07:38.210 --> 07:42.610
and in this way we save our changes actually in our original panel series.

07:42.610 --> 07:52.160
So let's run here and that's print our series and we can see that the changes we make here are in place.

07:52.190 --> 07:57.920
All right so now let's again create our dictionary and now this Monday Tuesday Wednesday and so on the

07:57.920 --> 08:07.180
strings and we can pass also this dictionary to uh PD dot series and create a panel series.

08:07.180 --> 08:09.200
Let's have a look here.

08:10.290 --> 08:16.790
And now if we sought the index with the salt index method then let's see what we get here.

08:18.650 --> 08:22.090
So then our index is actually sorted in an alphabet equal manner.

08:22.100 --> 08:28.280
So Friday starts with an F and then we have Monday with M first day Tuesday and Wednesday.

08:28.790 --> 08:33.110
So if we have a numerical index uh then the numerical index is sorted.

08:33.130 --> 08:39.620
Yeah from high to low and this a string index that's sorted uh alphabetically and we can also change

08:39.620 --> 08:42.790
here the order from ascending to descending.

08:45.260 --> 08:52.970
So now it's starting with uh the Wednesday w all right so now we are finished with this video and the

08:52.970 --> 08:59.150
next video will have a look at the methods and largest and smallest so hope to see you there by.
