Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Accessing Values in DataFrames

00:00 pandas also provides a way for you to access the data or the values of a DataFrame by using integer indices instead of, say, label indices. To do this, you have to use the .iloc accessor method. In order to do this, again, we need to specify two different indices separated by a comma. The first one is going to be the rows and the second one is going to be the columns.

00:26 This is now going to be exactly the same as you would when you’re accessing values from a NumPy array. So here, you have to use integers. So I could just pick up the first row, so this would be the first row of the DataFrame.

00:42 And I can also pick off only the first row but I want only the first and the third columns. So this would be just the first and third column of the first row. Now, if I wanted all of the rows of the first and third column, then I would just use the colon notation that would give me all of the rows.

01:07 So basically, you can use any of the slicing techniques that you would use for a NumPy array to access a sub-DataFrame of a DataFrame with the .iloc method.

01:19 Now, there’s sort of a weird thing that you have to keep in mind here. The actual index labels, the row labels of our DataFrame, are integers in this case, right?

01:28 They are from 10 to 16. Now, using the .iloc method, if I wanted to access say the first and the third rows, we would think, “Okay, maybe 10 and 12, and we want to get all of the columns.” Then I’m going to get an error, because 10 and 12these are not actual integer indices starting from 0 of this DataFrame. 10 and 12 are the actual row labels, which just happen to be integers.

02:01 So if I wanted to get that first row and also the third row, I would have to use these actual integers that correspond to the first row and the third row, of course always being zero-index-based.

02:17 All right, so that would work.

02:21 Now using .iloc and .loc, you can access individual cells or values of a DataFrame. So, for example, if I wanted to get the value in the row label 12, and maybe the city,

02:37 then I would simply pass in 12, 'city'. However, when you only need a single value like this, pandas recommends using specialized accessor methods called .at and .iat.

02:52 So with .at, you would use the same notation. So in this case, it would be 12 and 'city',

02:59 and this would pick off again, 'Prague'—that individual value. So this gives us the same thing as before. Now, with .at, you have to pass in again row labels and column labels, but if you wanted to pass integer-based indices, you would use .iat.

03:17 So row number 12, that was the third row, so we pass in an integer index of 2 and then city was the second column, and so we’d pass in a integer index of 1, and that would be 'Prague'.

03:33 So basically, .at and .iat are the same thing as .loc and .iloc, except here, you’re going to get individual elements always.

03:45 Now that we know how to access individual cells or values in a DataFrame or entire rows or columns, let’s talk about modifying the values of a DataFrame.

Become a Member to join the conversation.