WEBVTT

00:00.630 --> 00:06.080
In this video we learn how to sort data frames by column values and by index labels.

00:06.080 --> 00:14.070
So first of all we import pandas and we work with the Titanic's dataset and we will have a look at the

00:14.070 --> 00:16.620
first five frozen the last five rows

00:19.730 --> 00:23.160
and what we actually could do we could select a single column here.

00:23.180 --> 00:27.330
The H column and we can start the H column by the values.

00:27.340 --> 00:31.400
So let's first of all let's check here.

00:31.400 --> 00:39.370
Our H column so we get here a pen that's serious and we can see here that the values now append a serious

00:39.370 --> 00:41.000
set from the same order.

00:41.000 --> 00:49.090
Then in our original data frame starting from twenty to thirty eight twenty six and so on and with the

00:49.140 --> 00:55.660
salt values method we can actually start our pandas series.

00:55.700 --> 01:01.910
So now we can see here that our pandas serious starts with the peasant so that is 0 point for 2 years

01:01.910 --> 01:06.320
old and then we have four point six seven years.

01:06.320 --> 01:13.830
And on the left side we can see the indexed labour of the peasants in our original data frame and we

01:13.970 --> 01:21.120
the assault various method we not only can sort pandas serious but also pandas data frames.

01:21.380 --> 01:28.400
So the software use method does not only exist for serious but also for data frames so we can apply

01:28.400 --> 01:31.780
here the method directly to our data frame.

01:31.980 --> 01:38.300
And now if you want to start our complete data frame our complete rose by the H column we have to pass

01:39.750 --> 01:46.290
the label off the H column here in quotation marks to the method to salt values so let's check this

01:46.290 --> 01:46.760
out here

01:49.780 --> 01:55.870
and here we can see that we have the complete data frame sorted by the H column so starting with peasants

01:55.900 --> 02:02.620
a number eight hundred and three year that is 0 point for two years old and we can see here that by

02:02.620 --> 02:06.760
default we have from ascending order so starting from the youngest the peasants are.

02:07.150 --> 02:14.620
So let's check again our Titanic data set and print the first five rows and you can see here that our

02:14.800 --> 02:22.730
original data frame is not sorted so there must be obviously also a in place parameter in the sort that

02:22.730 --> 02:27.960
use method then let's check the parameters of this sort various method by pressing shift tab.

02:28.100 --> 02:32.000
And here we can see that we can start by the values along either axis.

02:32.030 --> 02:37.670
So we have to pass for the parameter by we have to pass that the column or the columns by which we want

02:37.670 --> 02:39.100
to sort our data frame.

02:39.500 --> 02:45.650
And this indeed is the in-place parameter which we have just set to true if you want to if you want

02:45.650 --> 02:52.950
to change our original data frame so let's set it to false here and we have the ascending parameter

02:53.010 --> 02:58.140
that we can define whether we want our data frame started in ascending or descending manner.

02:58.350 --> 03:05.940
So if you put here the parameter defaults we have a descending order and we have to define the axis

03:05.940 --> 03:07.540
along which we want to start.

03:07.590 --> 03:11.520
And in this example here we want to sort of a rose by the H column.

03:11.550 --> 03:12.780
So we want to sort our 0.

03:12.790 --> 03:17.050
So we have to pass out those 0 Art Index here and Secretary yet

03:21.040 --> 03:28.420
so here we have now our Titanic data frame sorted by the H column and then descending manner so starting

03:28.420 --> 03:38.310
from the oldest passenger and we can also change the Sierra again back to true so true as the default

03:38.310 --> 03:40.460
parameter here.

03:40.490 --> 03:42.760
So now we have an ascending order here.

03:42.980 --> 03:48.620
And once we are happy with our sorting we can then set the in-place parameter to true

03:52.490 --> 04:00.330
and let's check our data friends that we should have a new data frame here sorted by the H column and

04:00.360 --> 04:02.640
starting from the youngest passenger.

04:02.640 --> 04:05.530
So in a ascending order.

04:05.580 --> 04:12.060
All right so and b not only can thought our data frame by one column but also by many columns and therefore

04:12.090 --> 04:20.130
we can pass a list of column labels so we have passenger class sex and age and we set here the ascending

04:20.130 --> 04:21.550
parameter to fours.

04:21.560 --> 04:23.130
So let's see what we get here

04:27.270 --> 04:29.950
and what actually pendant does here at sorts.

04:30.030 --> 04:36.380
The data frame first by passenger class will starting from the first element then by sex.

04:36.420 --> 04:42.960
The second element and then by age and the third element in our list and we have a descending order

04:42.960 --> 04:45.210
so starting from height values.

04:45.420 --> 04:47.810
So let's have a look here at the passenger class column.

04:47.820 --> 04:50.390
So we are starting with class 3.

04:50.610 --> 04:56.550
So the highest value of the classes and then all passengers of class 3.

04:56.640 --> 05:02.230
Then ordered by the sixth column and alphabetically made it comes after a female.

05:02.240 --> 05:06.780
So we have a descending order starting with male and next.

05:06.840 --> 05:15.570
We as sort of a class the three males by the H also in descending order so starting from the oldest

05:15.570 --> 05:21.470
male until the youngest and let's have a check at the end of our data frame.

05:23.220 --> 05:25.760
So here we have the peasants class 1.

05:25.770 --> 05:34.360
We have females and ending actually at the youngest females here with the age to so the any end values

05:34.360 --> 05:35.910
are at the very end here.

05:35.920 --> 05:42.100
So to summarize we have sorted out our Titanic data frame first by the passenger class and then descending

05:42.100 --> 05:42.380
manner.

05:42.400 --> 05:45.090
Then by a and a descending man and then by H.

05:45.100 --> 05:46.180
And that is sending men.

05:46.810 --> 05:52.260
So what what if we want to sort our data frame again by the three columns here.

05:52.810 --> 05:56.730
But we want to start the passenger class in an ascending manner.

05:57.120 --> 06:01.000
And the sex in the descending manner and the H again in an ascending manner.

06:01.000 --> 06:06.340
And we can do this actually by not passing here at boolean value or false.

06:06.340 --> 06:12.100
True for all three columns but by passing here a list of boolean values.

06:12.250 --> 06:16.510
So let's assume we want to sort the peasants or class in an ascending manner.

06:16.510 --> 06:22.430
We have true then the sixth column in a descending manner falls.

06:22.700 --> 06:25.800
And then again the H column then an ascending manner.

06:25.830 --> 06:27.690
So also true.

06:27.740 --> 06:32.390
Let's see what we get here so now we are starting here.

06:32.390 --> 06:39.890
Passenger class from 1 soon an ascending manner then having all passengers of class 1 be Assad them

06:40.310 --> 06:42.460
by the gender starting from male.

06:43.570 --> 06:51.980
So we f here a descending order falls and then we are sliding all first class males by the H and then

06:52.070 --> 06:53.230
ascending manner again.

06:53.240 --> 07:01.010
So we feel truthful starting from the youngest male and let's again have a check here at the very last

07:01.030 --> 07:12.180
rows so here you have the peasants are classes 3 and we have females here and we would end actually

07:12.180 --> 07:16.270
at the oldest third class females.

07:16.320 --> 07:22.640
But now we have here at the end values at the very end but to summarize here so we canvassed for the

07:22.640 --> 07:29.720
site various method we can cite our data frame by a many columns and we can decide whether we want for

07:29.720 --> 07:34.510
each column in ascending or descending sliding all right.

07:34.510 --> 07:40.430
So now we're let's assume that we are happy with our sorting and then we can set our in-place parameter

07:40.430 --> 07:41.260
to a true

07:45.980 --> 07:49.250
and let's check in the first and the last rows here.

07:50.840 --> 07:59.030
So we have now 0 written our original data frame and we have our new data frame sorted by peasant class

07:59.690 --> 08:01.550
sex and age.

08:01.850 --> 08:07.910
And now let's assume that we are not happy with the sorting and we want to sort again our Titanic data

08:07.910 --> 08:10.030
frame by the index.

08:10.130 --> 08:16.330
So here we can see on the left side our data frame is starting with the index label 305 formed at forty

08:16.330 --> 08:25.840
five and so on and we can slide our data frame by the index label with the method thought index and

08:25.840 --> 08:29.700
again we have the option to have an ascending or descending manner.

08:29.710 --> 08:32.500
So we want to f our data frame sorted.

08:32.590 --> 08:41.160
Starting from 0 to 800 91 so we want to f in ascending order and we want to change our data frame back

08:41.160 --> 08:47.490
so we set to the in-place parameter to true and check again.

08:47.490 --> 08:53.460
So here we f again our it's no data frame you're starting with the peasant just with the index that

08:53.460 --> 08:56.390
Labor's 0 1 2 3 and so on.

08:56.380 --> 09:00.990
All right so now we are finished with this session and I hope to see you next on by.
