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
Instead of passing in a sequence type that would have three elements of
0, we can pass in one single value of
01:22 Go ahead and run that, and so now our DataFrame—the last column has those new values along the rows.
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.
This is just picking off the
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.
Let me add a few cells here just to push things up a little bit. Now, let me save in a row, and I’ll call this, say,
old_row. Let’s pick off the last row.
We’ll use the accessor method
.loc and the label is
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.
04:37 And so now the DataFrame—just like we had it.
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.
And if we ran that and then checked out our DataFrame, now the
city value in row labeled
11 has a value of
05:21 So, you see here how with DataFrames in pandas, modifying the values and accessing the values of a DataFrame is very similar to accessing and modifying the values of a NumPy array in NumPy.
05:35 All right! So up next, we’ll talk about inserting and deleting data from a pandas DataFrame.
Become a Member to join the conversation.