WEBVTT

00:01.120 --> 00:06.490
In this video I will provide you with a short summary of this section and also an outlook on the next

00:06.490 --> 00:12.970
section from the last videos we have learned how to import and how to initially inspect a data frame

00:13.270 --> 00:19.840
and also how to slice a select single elements and columns and rows of a data frame and we have also

00:19.840 --> 00:25.210
seen that for most of the tasks and pandas there is more than one way to do the same thing.

00:25.210 --> 00:32.060
However in most cases that's actually a good way to do something and that's a bad way to do something.

00:32.220 --> 00:38.330
And in this video my goal is to highlight and to show you the best practice solutions.

00:38.350 --> 00:44.300
All right so let's start now and whenever we work with pandas we have first of all import pandas as

00:44.310 --> 00:44.890
PD.

00:45.190 --> 00:54.820
So by conventional we import pandas SPDR and we can import data that is stored in a CSP file with the

00:54.880 --> 01:01.960
method PD that read CSA and we have to pass here the file name all right.

01:01.970 --> 01:02.780
So let's do this.

01:02.780 --> 01:06.050
And store here our data frame and the value of the summer

01:08.940 --> 01:15.210
and after having imported the data we need to perform a very first inspection to get a first impression

01:15.220 --> 01:19.360
how the data looks like and therefore we can never look at the very first five rows

01:22.280 --> 01:28.760
and we can also see here the column labels and we can also have a look at the last five rows with the

01:28.760 --> 01:29.540
tail method

01:32.570 --> 01:38.270
and we can call the info method to get to more information more meta information on our data frame

01:40.960 --> 01:47.530
so here we can see that we have over 30000 thousand rows and we have nine columns and we can see here

01:47.620 --> 01:49.840
the data types in each column.

01:49.840 --> 01:50.380
All right.

01:50.410 --> 01:56.260
So now the next step would be to inspect single columns and there are actually two ways to select single

01:56.260 --> 01:56.630
column.

01:56.630 --> 02:00.290
So on the one hand side we have the attribute mutation.

02:00.490 --> 02:09.040
So we can select the athlete column with the top athlete or we can use this map records notation and

02:09.040 --> 02:13.270
pass our column label here athlete within the snap records

02:17.880 --> 02:21.130
so both alternatives gives us actually the same result here.

02:23.270 --> 02:29.450
And the advantage of the attribute notation is actually that you can use the tap completion.

02:29.450 --> 02:37.440
However the attribute notation has some minor disadvantages which we will see later and this cost and

02:37.440 --> 02:45.550
you can also select multiple columns bypassing a list of the column labels within the span brackets

02:45.550 --> 02:45.850
here

02:50.680 --> 02:58.490
so here we have selected the year and the medal column and we can do the same with the log operator

02:58.980 --> 03:01.670
service the key operator we have to pass.

03:01.700 --> 03:07.970
There are labels that you want to select on the left hand side of the comma and the column labels on

03:07.970 --> 03:10.760
the right hand side of the comma here.

03:10.850 --> 03:18.400
And in this case we select all rows and we want to select the year and the middle column and we get

03:18.400 --> 03:19.580
to the same result.

03:19.760 --> 03:26.660
However I always strongly prefer the log notation and the reason for this actually the following.

03:26.660 --> 03:30.170
So if I want additionally select also a row.

03:30.200 --> 03:36.770
So for example let's assume that you want to select the row with the row label one then we can do this

03:36.770 --> 03:39.140
here both in our log operator.

03:39.470 --> 03:41.090
So we can pass here.

03:41.200 --> 03:43.930
They're all labeled one.

03:44.120 --> 03:47.870
And here we get for the medal with the arrow label 1 and the year.

03:47.900 --> 03:54.700
And the medal value so we can actually perform the two indexing operations so we select a row and we

03:54.700 --> 04:02.380
select columns within one indexing operation here at the lock operation and let's go up to our notation

04:02.380 --> 04:02.680
here.

04:02.680 --> 04:08.770
So now if you want to select here additionally the first row then gives you would need to change another

04:08.770 --> 04:11.110
indexing operation to select here.

04:11.140 --> 04:15.140
The row with them the index labeled one So for example we have to change the log.

04:15.150 --> 04:22.770
Operator and select the row with the index label 1 and actually we get to have the same results and

04:22.770 --> 04:23.900
both alternatives.

04:23.970 --> 04:30.490
However applying here the log operator with only 1 indexing operation here is definitely best practice

04:30.510 --> 04:36.570
and more idiomatic and what we did here actually we changed indexing operations so this was called chained

04:36.570 --> 04:41.970
indexing and later and this costs when it comes to manipulating elements in the data frame we will see

04:41.970 --> 04:51.020
that change indexing can lead to a significant confusion and problems so I got always you see the logo.

04:51.020 --> 04:51.480
I.

04:51.500 --> 04:55.970
Operator to select the single elements rows columns and whatever.

04:55.970 --> 04:58.310
So this is very powerful.

04:58.310 --> 05:02.970
All right so let's head on and we can select a rose with the eye log.

05:02.960 --> 05:07.610
Operator And we can perform here positional based indexing.

05:07.940 --> 05:16.860
So let's have a look here so we can select your all rows from an experience from 10 to 20 including

05:18.180 --> 05:21.990
and we can also perform label based indexing.

05:21.990 --> 05:29.570
So first of all let's say we import our summer dataset and we set the athlete column as the index column

05:32.910 --> 05:39.270
and then we can perform here label based indexing with the slope operator and we can pass an indexed

05:39.270 --> 05:42.270
label here within this bad records that we want to have.

05:42.270 --> 05:48.640
So is for example and then we get all instances of convoys.

05:48.660 --> 05:53.790
All right so now let's assume that we want to still all instances of Carl Lewis and we only want to

05:53.790 --> 06:01.630
have the year the event and the medal column and there are many ways to do it.

06:01.720 --> 06:06.280
So peanuts provides us here with full flexibility and many ways to do it.

06:06.280 --> 06:10.010
However as I said before there are good ways and bad ways to do it.

06:10.180 --> 06:11.950
And I show you here two bad ways to do it.

06:11.950 --> 06:14.120
And one good way to do it.

06:14.140 --> 06:19.510
So first of all we could slice our summer data frame by the three columns here.

06:19.510 --> 06:21.650
So let's cross here the rest out.

06:22.540 --> 06:30.250
So first of all we select the the three columns and all rows and then we can chain another index operation.

06:30.250 --> 06:33.100
So here selecting all rows with Carl Lewis

06:37.380 --> 06:42.140
and here we have all rows with conduits and we have the columns year event and medal.

06:42.960 --> 06:47.980
And we can also here rearrange our operations so we take first of all the log.

06:47.970 --> 06:50.850
Operator and select all rows with the conduits

06:55.310 --> 07:02.010
so here we have all rows with Carl Lewis and all columns of our data frame and then be contained and

07:02.010 --> 07:06.510
other indexing operation and can select the year the event and the middle column

07:11.080 --> 07:13.740
so actually here we get the same result as before.

07:13.930 --> 07:19.960
And we actually change to indexing operation so first of all we selected the rows with Carl Lewis and

07:19.960 --> 07:25.320
then we selected the three columns and now the third alternative how to do it.

07:25.330 --> 07:31.210
And the best alternative so I would highly recommend you to do your indexing and slicing operations

07:31.240 --> 07:32.110
in this way.

07:32.440 --> 07:39.370
So we use one indexing operation we use here the local operator and within this one indexing operation

07:39.370 --> 07:45.460
we select all rows with Carl Lewis and we select the columns year event the.

07:45.490 --> 07:51.840
So this is only one indexing operation and we get the same result here.

07:51.840 --> 07:58.260
And even if we do get four all three alternatives the same result the first two alternatives can cause

07:58.260 --> 08:04.740
severe problems later when we try to change and manipulate elements in our data frame because he in

08:04.740 --> 08:08.490
the first two identifies the with the change indexing.

08:08.490 --> 08:11.470
So we chained one index operation after another.

08:11.760 --> 08:15.110
Here we have our first index operation and the second one.

08:15.360 --> 08:23.630
And here we have only one index Operation All right so now let's move on and let's have a look again

08:23.630 --> 08:31.310
at our first five rows and append US library provides us actually with the three different data types

08:31.310 --> 08:37.910
or classes and we already learned that here our summer data frame is a data frame so this is actually

08:37.910 --> 08:42.190
the first the data type and then if we select one column here.

08:42.190 --> 08:48.530
So for example the year column and we can see here that this is a pattern that serious.

08:48.560 --> 08:54.420
So it's a one dimensional labeled array and the data type is here a that serious.

08:55.130 --> 09:00.860
And if you select here the column labels lists the attribute columns.

09:00.860 --> 09:06.980
And if you check your also for the data type then we can see here that this is a panda's index object

09:09.020 --> 09:14.370
and we can also call the ROW Labels with the attribute index.

09:14.570 --> 09:23.670
And this is also an index object before we move on this data frames in the next section we will have

09:23.670 --> 09:27.540
a deeper look into pandas series and pandas index objects.

09:27.570 --> 09:32.800
So these are both very important topics then we later work again with their friends.

09:33.000 --> 09:37.830
So hope you enjoyed this first section and I hope to see you in the next section by.
