Accessing Values in DataFrames
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.
They are from
16. Now, using the
.iloc method, if I wanted to access say the first and the third rows, we would think, “Okay, maybe
12, and we want to get all of the columns.” Then I’m going to get an error, because
12—these are not actual integer indices starting from
0 of this DataFrame.
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.
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
Become a Member to join the conversation.