In the previous part we introduced the Series class, which is one of the fundamental data types in pandas. Let’s quickly recreate the Series object from the previous part:
import numpy as np
import pandas as pd
nums = pd.Series([2.35, 4.11, 0.87, 2.76, 3.12, 5.79])
nums
ARBITRARY INDICES
This Series was created with just one argument, the list of float numbers. This is why it has the default indexing starting at 0 with values increasing by one. But you can define the index explicitly, passing it as the second argument. The values do not have to be in order:
nums = pd.Series([2.35, 4.11, 0.87, 2.76, 3.12, 5.79],
index = [3, 4, 5, 8, 7, 6])
nums
What’s more, index value may be even repeated:
nums = pd.Series([2.35, 4.11, 0.87, 2.76, 3.12, 5.79],
index = [4, 4, 4, 6, 9, 6])
nums
Finally, they don’t have to be numbers at all. They can be strings if it makes sense:
nums = pd.Series([2.35, 4.11, 0.87, 2.76, 3.12, 5.79],
index = ['a', 'b', 'c', 'd', 'e', 'f'])
nums
Or, just for demonstration purposes, something like this:
nums = pd.Series([2.35, 4.11, 0.87, 2.76, 3.12, 5.79],
index = [True, False, 'index 3', (1, 2, 3), ['a', 'b'], {1: 'a', 2: 'b'}])
nums
This example is not very practical, but it demonstrates that you can use any data type as the index.
If you use indices, which are not integers, you can also use them to access elements and for slicing:
nums = pd.Series([2.35, 4.11, 0.87, 2.76, 3.12, 5.79],
index = ['A', 'B', 'C', 'D', 'E', 'F'])
# access element at index E
nums['E']
# slice from element at index 'C' to the end
nums['C':]
SERIES FROM DICTIONARY
A more practical example would be creating a Series from a dictionary. Then the keys will be the indices:
# a dictionary
salary_dict = {'Martha Williams': 40000,
'Ben Hacker': 36000,
'Michael Johnson': 39500,
'Tess Danvil': 42600,
'Emily Prat': 32800}
# a Series
salaries = pd.Series(salary_dict)
salaries
Naturally, you can use these indices to access elements and for slicing:
# access Tess Danvil's salary
salaries['Tess Danvil']
# slice from Ben Hacker to the end, every other element
salaries['Ben Hacker'::2]
You can use dictionary methods to access just the keys or key-value pairs as tuples:
# just the keys
salaries.keys()
# key-value pairs as tuples
list(salaries.items())
If you want to check whether a particular key is in the Series object, you can use the membership operators:
'Ben Hacker' in salaries
'Steve McKenzie' not in salaries
You can also modify Series objects and add new ones just like you do with dictionaries:
# let's modify an object
salaries['Tess Danvil'] = 49000
# let's add a new object
salaries['Steve McKenzie'] = 31400
salaries
If you create a Series from a dictionary, you can explicitly indicate which keys you want to be used, how many times and in what order:
pd.Series({'a': 74, 'b': 32, 'c': 21, 'd': 72, 'e': 81, 'f': 36},
index = ['d', 'f', 'e', 'f'])
EQUAL VALUES
Finally, if you want all the values to be equal, you can create a Series from a scalar:
pd.Series(2.5, index = [10, 20, 30, 40, 50, 60])
Here’s the video version of this article: