Modifying Values in DataFrames: Accessor Methods
00:00
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.
00:18
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 py-score
column.
00:43
This would correspond to changing four rows. Let’s suppose we change these to 40
, 50
, 60
, and 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 0
.
01:13
Instead of passing in a sequence type that would have three elements of 0
, we can pass in one single value of 0
.
01:22 Go ahead and run that, and so now our DataFrame—the last column has those new values along the rows.
01:32
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.
01:53
This is just picking off the py-score
column.
01:58
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 20
and 50
has to be equal to the number of rows in our DataFrame.
02:26
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.
02:59
That changes the last column. And so if we take a look at the DataFrame, now we’ve got that last column starting at 20
to 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:19
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.
03:29
We’ll use the accessor method .loc
and the label is 16
.
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.
03:48
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.
04:17
The last row has that new data where the name is Jack
, Chicago
, 29
, and 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.
04:43
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.
05:10
And if we ran that and then checked out our DataFrame, now the city
value in row labeled 11
has a value of Ottawa
.
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.