### Pandas Series¶

In [1]:
# Import library of pandas and numpy
import numpy as np
import pandas as pd


### Creating a Series¶

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

In [2]:
# Defining values to use in later stage
labels = ['a','b','c']
my_list = [10,20,30]
arr = np.array([10,20,30])
d = {'a':10,'b':20,'c':30}


Using above created Lists

In [3]:
pd.Series(data=my_list)

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

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

Out[6]:
a    10
b    20
c    30
dtype: int64

NumPy Arrays

In [11]:
pd.Series(arr)

Out[11]:
0    10
1    20
2    30
dtype: int32
In [12]:
pd.Series(arr,labels)

Out[12]:
a    10
b    20
c    30
dtype: int32

Dictionary

In [13]:
pd.Series(d)

Out[13]:
a    10
b    20
c    30
dtype: int64

### Data in a Series¶

A pandas Series can hold a variety of object types:

In [14]:
pd.Series(data=labels)

Out[14]:
0    a
1    b
2    c
dtype: object
In [15]:
# Even functions (although unlikely that you will use this)
pd.Series([sum,print,len])

Out[15]:
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 [16]:
ser1 = pd.Series([1,2,3,4],index = ['USA', 'Germany','USSR', 'Japan'])

In [18]:
ser1

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

In [20]:
ser2

Out[20]:
USA        1
Germany    2
Italy      5
Japan      4
dtype: int64
In [21]:
ser1['USA']

Out[21]:
1

Operations are then also done based off of index:

In [22]:
ser1 + ser2

Out[22]:
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 next chapter for DataFrames, which will expand on the concept of Series!

In [ ]: