WEBVTT

00:01.220 --> 00:05.420
In this video you will learn when and why to use the copy method.

00:05.420 --> 00:10.460
So first of all we import the pandas and we import our Titanic data set.

00:10.460 --> 00:17.300
So that's the first five rows here and in the last videos we selected a numerical or a non numerical

00:17.300 --> 00:21.270
column and we analyzed the column and played around with it.

00:21.290 --> 00:28.610
That's a typical workflow with pandas selecting a subset of a data frame for example a column and playing

00:28.610 --> 00:29.590
around with it.

00:29.670 --> 00:36.800
But actually we do not want to change the original data frame and that sounds easy but there is a severe

00:36.800 --> 00:42.780
pitfall here that causes one of the most frequently made mistakes when working with pandas.

00:42.950 --> 00:49.700
Again we select here the H column and save for the H column and the variable h here and let's have a

00:49.700 --> 00:49.880
look.

00:49.880 --> 00:52.820
So these are the first five elements.

00:52.820 --> 00:55.370
And let's assume that we want to change.

00:55.460 --> 00:58.340
The third element here at the next position too.

00:58.730 --> 01:04.880
And the peasants of here is not the 26 year old but the 29 years old and we can actually change and

01:04.940 --> 01:08.450
overwrite that element by selecting the element here.

01:08.450 --> 01:13.760
For example with the ILO operator and we assign the new value twenty nine.

01:13.940 --> 01:16.000
So let's do this here.

01:16.700 --> 01:19.110
And here we get a warning message.

01:19.190 --> 01:22.610
This so-called setting with copy warning.

01:22.610 --> 01:28.130
And it says here a value is trying to be set on a copy of a slice from a data frame.

01:28.130 --> 01:31.270
So this sounds weird actually but let's simply have a look.

01:31.400 --> 01:33.230
What we did here with our code.

01:33.440 --> 01:36.890
So we have a look here first of all on our H column here.

01:37.040 --> 01:39.120
So that's the five elements.

01:39.290 --> 01:44.750
And we can see here that we changed to the third element from 26 to 29.

01:44.870 --> 01:51.830
And let's also look here at our Titanic data frame and the intention was actually not to change the

01:51.830 --> 01:57.340
element here in the overall data frame but that's exactly what we did here.

01:57.350 --> 02:02.960
So also here we have 29 and that's really a severe pitfall yet pandas.

02:03.080 --> 02:07.780
So we only intended to change one element in a slice of a data frame.

02:07.790 --> 02:09.280
So we in our each column.

02:09.500 --> 02:10.480
But the.

02:10.520 --> 02:17.930
We effectively changed also the overall data frame and there's a simple back around to prevent this

02:17.930 --> 02:19.180
behavior here.

02:19.370 --> 02:21.130
And let's go up again here.

02:21.830 --> 02:24.350
So let's rerun here the sets.

02:24.370 --> 02:26.300
So again we're in part here.

02:26.300 --> 02:28.880
The Titanic is yes we file.

02:28.880 --> 02:35.390
So again here we have 26 and now we want to select the H column and we want to play around with the

02:35.390 --> 02:41.110
H column but we do not want to change to the original data frame and that's a simple background.

02:41.120 --> 02:43.730
So we are changing the copy method

02:47.300 --> 02:50.270
and by doing so pen creates a separate object.

02:50.270 --> 02:55.180
So our each column that is stored in the variable H is completely independent.

02:55.190 --> 03:02.290
Now from our Titanic data frame and before without the copy method the object that was stored in the

03:02.290 --> 03:08.720
verbal H was not independent from the data frame and in fact the object that was stored in the available

03:08.720 --> 03:16.910
H was a view on the data frame so it was actually still part of the overall data frame and we can prevent

03:16.910 --> 03:21.350
this by creating a copy here and let's do this here

03:24.440 --> 03:27.500
and let's repeat the process that we changed here.

03:27.500 --> 03:30.700
The third element 229.

03:30.850 --> 03:36.650
And here we can see we do not get any warning message and it's also checking the consequences on the

03:36.650 --> 03:40.070
object that is start here in the variable H.

03:40.100 --> 03:43.390
So here again we have changed to the third element.

03:43.580 --> 03:47.730
But know our original data frame we did not change the elements.

03:47.730 --> 03:50.350
So here we have still 26.

03:50.450 --> 03:53.720
So that's the most important use case for the copy method.

03:53.720 --> 03:57.410
And with this we are finished here and I hope to see us on the next video by.
