Modifying Values in DataFrames: Accessor Methods
Let’s now go over how we would modify the values in our DataFrame using these accessor methods. So using the
.iloc and the
.loc methods, we can modify parts of our DataFrame by passing in, say, a sequence or a NumPy array, or even just a single value.
For example, here’s our DataFrame, and let’s suppose that we wanted to change the
py-score column. Say that we wanted to change the rows that are from the first row up until the row with label
13—so in this case, we would be changing the first four rows—and we want to change the
This would correspond to changing four rows. Let’s suppose we change these to
70. And then we also wanted to change the rows that follow, so this would be from the row with label
14 all the way up until the last row. And we want to change, again, the same column
py-score. And then after this, we want all of the rows to be set to
Now let’s do something similar, but this time, let’s use the
.iloc accessor method. Let’s change the last column, so I want all of the rows of the last column, and I’m going to use the
-1 index, which is similar notation to how you would access the last element of a list.
And this time, what we’ll do is we’ll modify this column using a NumPy array. I’m going to use the
linspace() method, which creates a NumPy array from an initial value, say of
20, all the way up to
50, and the number of values between the range
50 has to be equal to the number of rows in our DataFrame.
If we use the length method on the DataFrame, this is going to give us the number of rows of the DataFrame. So if you’re not familiar with the
linspace() function in NumPy, what this does is it returns a NumPy array where the elements in the array are evenly spaced numbers over some specified interval. So in this case, we want the interval to start at
20 and then end at
50, and then it’ll return that many points evenly spaced throughout that interval. So, let me run that so you can see it.
That changes the last column. And so if we take a look at the DataFrame, now we’ve got that last column starting at
50, equally spaced, so in this case, the step size had to be five. Now, of course, we can also change rows, so let’s change maybe a row.
03:37 And I want to save that row because I want to make sure that I put my DataFrame back to how it was, and this will also just be more practice with accessing and saving rows and reassigning values to rows.
So what I’m going to do is I’m going to change that last row with label
16 to a new row that contains, say, the name
'Jack', and Jack is from
'Chicago', and Jack is
29 and scored
70 in his Python test. So that’s going to reassign the row at label
16, and let’s also view the DataFrame after we modify that last row.
The last row has that new data where the name is
70. I can go ahead and change that row back to like it was, so I’m going to use the same accessor method, label
16, and let’s just put the old row back to where it was.
Now, of course, if we wanted to change individual cells of a DataFrame, we could use, say, the
.loc method if we knew the row labels. So for example, let’s suppose we wanted to change the city for the row label
11, which corresponds to
Ann, so this is row label
11, and we want to change the
'city' value, and Ann is not actually from Toronto, she is from Ottawa.
Become a Member to join the conversation.