There are multiple ways to get to the data that is stored in your Series
. Let's explore the balances
Series
.
Remember, the Series
is indexed by username. The label is the username, the value is that user's balance.
# Setup
import pandas as pd
from utils import render
# Standard Python dictionary
test_balance_data = {
'pasan': 20.00,
'treasure': 20.18,
'ashley': 1.05,
'craig': 42.42,
}
balances = pd.Series(test_balance_data)
A Series
is ordered and indexable. It is zero based and you can access it by index, just like you would a list or array.
# Get the first user's balance
balances[0]
20.0
Let's take a loook at the value returned.
type(balances[0])
numpy.float64
The value is wrapped in a NumPy.Scalar
so that it keeps it's data type and will play well with others.
The same positional indexing works just as it does with a standard list.
# The last balance
balances[-1]
42.42
Since a series is labelled, you can also access it much like you would a standard dict
.
balances['pasan']
20.0
Series
behave like dictionaries¶try:
balances['kermit']
except KeyError:
render('Accessing a non-existent key raises a `KeyError`.')
Accessing a non-existent key raises a KeyError
.
if balances.get('kermit') is None:
render('Use `get` to safely access keys. `None` is returned if key not present.')
Use get
to safely access keys. None
is returned if key not present.
if 'kermit' not in balances:
render('Use `in` to test the existence of a label.')
Use in
to test the existence of a label.
As long as your label meets variable naming constraints, it will be available as a property via dot notation on the Series
!
balances.ashley
1.05
We are using indexing which can either be a label or a positional index. This can get confusing. It's possible to be more explicit, which yes wise Pythonista, is always better than implicit.
A Series
exposes a property named loc
which can be used to explicitly lookup by label based indices only.
balances.loc['pasan']
20.0
And to use the positional index explicitly, you can use the property iloc
.
# Get the first value
balances.iloc[0]
20.0
Like a NumPy array, a Series
also provides a way to use slices to get different portions of the data, returned as a Series
.
NOTE: Slicing with indices vs. labels behaves differently. The latter is inclusive.
When using positional indices, the slice is exclusive...
# Includes values from zero
# up until **and not** including 3
balances.iloc[0:3]
pasan 20.00 treasure 20.18 ashley 1.05 dtype: float64
When using labels, the slice is inclusive...
# Include the values starting at 'pasan'
# up until **and** including 'ashley'
balances.loc['pasan':'ashley']
pasan 20.00 treasure 20.18 ashley 1.05 dtype: float64