Series

The first main data type we will learn about for pandas is the Series data type. Let's import Pandas and explore the Series object.

A Series is very similar to a NumPy array (in fact it is built on top of the NumPy array object). What differentiates the NumPy array from a Series, is that a Series can have axis labels, meaning it can be indexed by a label, instead of just a number location. It also doesn't need to hold numeric data, it can hold any arbitrary Python Object.

Let's explore this concept through some examples:

In [1]:
import numpy as np
import pandas as pd

Creating a Series

You can convert a list,numpy array, or dictionary to a Series:

Syntax

pandas.Series(data, label)

Creating a Series From Lists

In [2]:
my_list = [10,20,30]
pd.Series(data=my_list)
Out[2]:
0    10
1    20
2    30
dtype: int64
In [3]:
my_list = [10,20,30]
labels = ['a','b','c']

pd.Series(data=my_list, index=labels)
Out[3]:
a    10
b    20
c    30
dtype: int64

The above is equivalent to:

In [4]:
my_list = [10,20,30]
labels = ['a','b','c']

pd.Series(my_list, labels)
Out[4]:
a    10
b    20
c    30
dtype: int64

Creating a Series From NumPy Arrays

In [5]:
arr = np.array([10,20,30])
pd.Series(arr)
Out[5]:
0    10
1    20
2    30
dtype: int32
In [6]:
arr = np.array([10,20,30])
labels = ['a','b','c']
pd.Series(arr,labels)
Out[6]:
a    10
b    20
c    30
dtype: int32

Creating a Series From Dictionary

In [7]:
d = {'a':10,'b':20,'c':30}
pd.Series(d)
Out[7]:
a    10
b    20
c    30
dtype: int64

Data in a Series

A pandas Series can hold a variety of object types:

In [8]:
labels = ['a','b','c']
pd.Series(data=labels)
Out[8]:
0    a
1    b
2    c
dtype: object

A pandas Series can Even hold functions (although unlikely that you will use this)

In [9]:
pd.Series([sum,print,len])
Out[9]:
0      <built-in function sum>
1    <built-in function print>
2      <built-in function len>
dtype: object

Using an Index

The key to using a Series is understanding its index. Pandas makes use of these index names or numbers by allowing for fast look ups of information (works like a hash table or dictionary).

Let's see some examples of how to grab information from a Series. Let us create two sereis, ser1 and ser2:

In [10]:
ser1 = pd.Series([1,2,3,4],index = ['USA', 'Germany','USSR', 'Japan'])                                   
ser1
Out[10]:
USA        1
Germany    2
USSR       3
Japan      4
dtype: int64
In [11]:
ser2 = pd.Series([1,2,5,4],index = ['USA', 'Germany','Italy', 'Japan'])                                   
ser2
Out[11]:
USA        1
Germany    2
Italy      5
Japan      4
dtype: int64

Access Data in a Series

You can access data in a Series via Bracket Notation []

In [12]:
ser1 = pd.Series([1,2,3,4], index = ['USA', 'Germany', 'USSR', 'Japan'])
ser1['USA']
Out[12]:
1
ser1['Canada'] # KeyError: 'Canada'

Operations on Series

Operations are done based on index. In the example below, if there is a match, add index value of together. if there is no match, return NaN

Operations are then also done based off of index:

In [14]:
ser1 = pd.Series([1,2,3,4], index = ['USA', 'Germany', 'USSR', 'Japan'])
ser2 = pd.Series([1,2,5,4], index = ['USA', 'Germany', 'Italy', 'Japan'])
ser1 + ser2
Out[14]:
Germany    4.0
Italy      NaN
Japan      8.0
USA        2.0
USSR       NaN
dtype: float64

Let's stop here for now and move on to DataFrames, which will expand on the concept of Series!