WEBVTT

00:01.390 --> 00:05.520
In this video we learn how to change our ROE index in a data frame.

00:05.560 --> 00:07.930
And we have already seen that by default.

00:07.930 --> 00:13.540
We have a range index starting from zero but in many cases it might be beneficial to ask a different

00:13.540 --> 00:18.050
DRO index that enables us to label based indexing.

00:18.210 --> 00:25.600
So but first of all let's import pandas and we import our Summer Olympic Games data set and start in

00:25.600 --> 00:27.000
the variable summer.

00:27.820 --> 00:34.060
And we have a look at the first five rows and we have a look at the index.

00:34.070 --> 00:41.310
So we have a range index starting from zero and we have already learned that we can change the index

00:41.340 --> 00:48.810
and select a column as the index and imparting our data with the read CSP method so we can say here

00:49.590 --> 00:56.870
index column equals and let's say we want to have the athlete column as our index column so we can pass

00:56.880 --> 00:57.480
athlete

01:02.970 --> 01:09.570
and now we have few on the left side our athlete column s and our index off often the data frame and

01:09.570 --> 01:11.280
we can check the index here

01:14.200 --> 01:19.580
and here we can see our index is now at the athletes column.

01:19.600 --> 01:25.360
So now what if we are not happy with our index and we want to change it back to the Range Index and

01:25.450 --> 01:29.450
therefore we have the method reset index.

01:29.650 --> 01:39.760
So let's just try it out here and we can see here on the left side we again half our Range Index and

01:39.790 --> 01:41.270
our athlete column.

01:41.310 --> 01:49.060
Tom Speck as a column so it's not any more index but a normal column of our data frame and we can also

01:49.060 --> 01:57.030
check here the parameters and we have one parameter drop equals force.

01:57.310 --> 02:01.020
So this is set by default to force.

02:01.180 --> 02:03.090
So let's try it out again.

02:03.190 --> 02:11.200
And what if he set this to true here then we completely drop our index that we had before.

02:11.200 --> 02:17.070
So before we head to the athlete column as our index and if he said that the parameter dropped too true

02:17.110 --> 02:23.380
we completely drop our index and replace it by the range and next and you can see here there is no athlete

02:23.380 --> 02:28.810
column in our data frame so that's uh in this case actually not what we uh want.

02:28.840 --> 02:33.470
So we change it back to false.

02:33.480 --> 02:39.250
Now again we have our athlete column and our data frame and also in the Reset index method and there

02:39.260 --> 02:45.360
is and the in-place parameter so by default it's set to fall.

02:45.390 --> 02:51.510
So we do not change our original data frame and if we want to change our original data frame and change

02:51.510 --> 02:57.560
the index back to the Range Index then we have to set of the in-place parameter to true.

02:58.650 --> 03:04.650
So now if you run here so now we can see here that we do not get a view on a copy.

03:04.640 --> 03:10.470
So this is a good indication that we changed our original data frame and let's check with the method

03:11.050 --> 03:12.860
and then we can see here on the left side.

03:12.870 --> 03:20.130
Again we have a Range Index and still we have our athlete column as a normal column of our data frame.

03:20.190 --> 03:25.370
So now let's assume that before we we are not happy with the athlete column as in the index.

03:25.410 --> 03:31.550
But now we want to have the year column as our index and there are actually two ways to do it.

03:31.550 --> 03:39.140
So either we do this already when we import the data and set here the index card to year or if we have

03:39.140 --> 03:40.050
already.

03:40.510 --> 03:47.810
Well if we have already imported our data that is in the set index method to do this and let's press

03:47.810 --> 03:50.360
shift tap here.

03:50.360 --> 03:56.450
So the set next method to set to the data frame index using one or more existing columns.

03:56.450 --> 04:01.700
So what we can do here actually we can pass them the column that we like to have as the index.

04:01.700 --> 04:09.790
So in this case here in quotation marks and let us see what we get here then you're on the left side

04:09.820 --> 04:13.750
as in the index we have in the year column.

04:13.860 --> 04:20.550
All right so let's again press shift tap here and that's a parameter called drop.

04:20.670 --> 04:24.500
And by default drop is set to true.

04:24.540 --> 04:30.300
That means if we accept that the year column mass index it will be removed as an ordinary column.

04:30.510 --> 04:37.530
And if we change this and accept the drop curve parameter to force the year column and not be removed

04:37.590 --> 04:38.470
as a column.

04:38.490 --> 04:43.130
So in the end we have the year column twice as an index and as an ordinary column.

04:43.290 --> 04:48.170
So let's check this out.

04:48.290 --> 04:50.090
So by default it's set to true

04:53.020 --> 05:02.730
and we can see that we do not have a year column as column but if we accept this to false then we still

05:02.730 --> 05:06.750
have the year column as column and s index.

05:06.840 --> 05:09.060
So let's change it back here to true

05:11.750 --> 05:16.890
and uh let's set up the in-place parameter to a true

05:21.610 --> 05:30.330
and let's say again our data frame and now we have from the year column as index and we actually removed

05:30.410 --> 05:32.040
the column from the column.

05:32.050 --> 05:38.650
So there's no column year and now we can also check if the index is unique.

05:38.870 --> 05:44.960
So I would assume that we get a false because already here we can see that we have a duplicate value

05:44.990 --> 05:49.250
so eighteen hundred ninety six.

05:49.340 --> 05:49.880
All right

05:52.820 --> 05:59.660
so now we have learned how to change the complete index so we can reset our index to a range index and

05:59.660 --> 06:02.650
we can set a complete column as our new index.

06:02.680 --> 06:10.300
But what if you want to change only one element of our index and we can try this so let's have a look

06:10.300 --> 06:13.000
here at our summer index.

06:13.000 --> 06:16.560
So we fear our year columns index and the few select them.

06:16.600 --> 06:22.800
The first element so here it's in the first element 896.

06:22.840 --> 06:30.430
And if you try to set a new values of for example eighteen hundred ninety four when we get here an error

06:30.430 --> 06:39.870
message index does not support mutable operations so with other words the index objects immutable and

06:39.870 --> 06:45.990
we can also tried to set a new value for all elements of the index object so-called broadcasting so

06:45.990 --> 06:50.410
we can say that for all elements in our index we want to set them the new value.

06:50.550 --> 06:58.060
So the string here before 2016 and we can try this and again we get here an error message.

06:58.690 --> 07:05.880
So this doesn't work but what we can do we can pass a complete list or sequence and that has exactly

07:05.880 --> 07:08.960
the same amount of elements as our index.

07:08.960 --> 07:10.340
Then as the new index.

07:10.340 --> 07:13.710
So first of all let's check the size of our index.

07:13.710 --> 07:18.810
So we have thirty one thousand one hundred sixty five elements in our index.

07:18.810 --> 07:26.600
So if we pass a list or a sequence with is thirty one thousand one hundred sixty five new elements as

07:26.610 --> 07:29.250
our new index then this should work.

07:29.250 --> 07:35.070
And therefore we're creating here a list with thirty one thousand one hundred sixty five elements and

07:35.070 --> 07:36.930
we want to have a new label.

07:36.930 --> 07:41.280
And starting from medal number one better number two medal number three and so on.

07:41.280 --> 07:47.370
So we want to Numa rate our medals from medal number one and two medal number thirty one thousand one

07:47.370 --> 07:48.390
hundred sixty five.

07:48.390 --> 07:53.360
And therefore we use here a list comprehension.

07:53.430 --> 08:01.190
So for Ian Raines one to thirty one thousand one hundred sixty five including we name our elements and

08:01.230 --> 08:04.620
our new list medal number one and number two and so on.

08:04.620 --> 08:07.860
So now let's create our list here and let's have a look

08:10.620 --> 08:12.620
so here you can see our list.

08:12.630 --> 08:21.440
So it starts with medal number one and yeah in the end we're ending with a medal number thirty one thousand

08:21.440 --> 08:23.190
one hundred sixty five.

08:23.260 --> 08:30.590
And what we now can do we can say OK we want to set our index to a new index so new index here is our

08:30.590 --> 08:34.910
list and let's try out if this works.

08:34.910 --> 08:35.350
All right.

08:35.360 --> 08:42.820
We do not get an error message and let's check in the first five rows and the last five rows and two

08:42.850 --> 08:49.690
we can see that we have set our list new index as the index of our data frame and also check the last

08:49.690 --> 08:50.440
five for us.

08:51.070 --> 08:56.210
So starting from medal number one to the medal number thirty one thousand one hundred sixty five.

08:56.350 --> 09:00.670
And we can also check if our row index only has a unique value.

09:00.670 --> 09:03.430
So I would assume so yes.

09:03.450 --> 09:03.860
All right.

09:03.870 --> 09:12.420
So and that's a in addition a attribute called index stock name and let's check if we have a name already

09:12.420 --> 09:16.240
for our index and here we run this.

09:16.280 --> 09:22.460
And we see nothing so that's them at this point no name for our new index button but we can set a new

09:22.460 --> 09:26.810
name by assigning for example a medal number.

09:26.810 --> 09:36.980
So let's try this out and let's again check in the first five rows here and now here we can see that

09:37.130 --> 09:39.140
we have a name for our index.

09:39.170 --> 09:44.640
So we have medal number and now if we apply again the reset index method

09:49.150 --> 09:54.970
we can see here on the left that we again have a range index and our index that we had before here medal

09:54.970 --> 09:55.440
numbers.

09:55.450 --> 10:02.140
Now a column of our data frame with the column label medal number.

10:02.190 --> 10:06.600
All right so now we are finished service this video and I hope to see you on the next video by.
