Accessing Data in a Series
00:00 You’ve been working with Series and DataFrames but sometimes you need to get access to specific values. In Pandas, there are several ways to do this, and first is with the indexing operator.
00:12
A Series
will always have a RangeIndex
, which simply provides positional indices to the values. This index is zero-based, just like a Python list, so to get the second value in the city_revenues
Series
, you would ask for index 1
.
00:32
The city_revenues
Series
also has an explicit index that maps a label to each row. Thus, to get the value for 'Toronto'
, you could also use the indexing operator.
00:45
Other Python list functionality is applied to Series
, such as negative indices to start from the end of the sequence. You can even slice a Series
using the labels.
00:57
Use a colon (:
) to slice all values starting at 'Toronto'
with no upper value.
01:04
For more access options, you can turn to the .loc
and .iloc
properties. Consider the following.
01:13
What value will the second index in colors
return? Again, every Series
will have an implicit RangeIndex
that lets you use positional indexes. However, colors
uses labels that are also integers.
01:30
It turns out that the labels take precedence and that colors[1]
returns the value 'red'
. But Pandas uses the .loc
and .iloc
attributes to make things less confusing.
01:43
Using the index operator with .loc
refers to the labels. In this case, colors.loc[1]
returns 'red'
, as the label 1
in the index is mapped to the value 'red'
.
01:58
But .iloc
refers to the positional index, so colors.iloc[1]
returns 'purple'
.
02:08
You can also slice Series
using .loc
and .iloc
. Slicing using .iloc
works the same way as slicing a list.
02:16
The upper bound is exclusive. Slicing with .loc
works the same way as a list with one exception: the upper bound is inclusive.
02:28
Remember that the values supplied to .loc
are labels. Between 3
and 8
inclusive, four colors, there are only three labels: 3
, 5
, and 8
.
02:40
You can also use negative indices with .iloc
only.
02:47
If you come across some older Pandas apps, you may see a .ix
indexer. This would accept either a positional or label index and then Pandas has to figure out which one was correct.
02:58
It caused a lot of confusion, so it was deprecated in version 0.20.0. Even though you may see it, you should not use it, and stick with .loc
and .iloc
.
03:12
In summary, using .iloc
on a Series
behaves similar to a list and .loc
is similar to a dictionary in Python. And that’s how you access data in a Series
! In the next lesson, you’ll see how to do the same but with a DataFrame
.
Become a Member to join the conversation.
robertportelli on Oct. 17, 2023
This is the best setup and explanation of
.loc
,.iloc
, and.loc
vs.iloc
.