Series  

  • Series is a one-dimensional array-like structure containing homogeneous data.

  • Example of a series:

      10 	23    56    17    52   61   73    90    26    72
    

  • Key Points:

    • Homogeneous data (all elements in series has the same data type)

    • Size of a series is immutable

    • Values stored in a series are mutable

  Defining a series object  

  • Syntax:

      seriesVar = pandas.Series( data, index, dtype, copy)
    

    The parameters of the constructor are:

    • data:   various forms, e.g.: a list

                   Default:  [ ]

    • index:   index values must be unique and hashable, same length as data.

                   Default:   np.arrange(n), i.e.: [0 .. (n-1)]

    • dtype:   data type.

                   Default:   data type is inferred from data

    • copy:   copy data.

                   Default:   False

  Creating Series objects  

  • How to create an empty series:

    #import the pandas library and aliasing as pd
    import pandas as pd
    
    s1 = pd.Series()
    print(s1)                 # Output: Series([], dtype: float64)
    
    


  • How to create a series with a Python list:

    #import the pandas library and aliasing as pd
    import pandas as pd
    
    s2 = pd.Series([9, 8])
    print(s2)                 
    
    Output:    0    9             (0, 1 are indexes)
               1    8
               dtype: int64
    

DEMO: progs/series01.py

  Using your own indexes  

  • How to create series with your own indexes:

    data = ['a', b', 'c']
    
    s1 = pd.Series(data,index=[100,101,99])
    print(s1)
    
    Output:
              100    a
              101    b
              99     c
              dtype: object
    
    

  • You can use symbols as index:

    s2 = pd.Series(data, ['x', 'y', 'z'])
    print(s2)
    
    Output:
            x    a
    	y    b
    	z    c
    	dtype: object
    

  Create a Series from dict  

  • A dict can be used as input to construct a series

    • If no index is specified:

      • the dictionary keys are used as index

    • If index is passed:

      • the values in the input data corresponding to the labels in the index will be pulled out

    Example: no index specified

    data = {'a' : 0.0, 'b' : 1.0, 'c' : 2.0}
    s = pd.Series(data)
    
    print(s)
    
    Output:
            a    0.0
    	b    1.0
    	c    2.0
    	dtype: float64
    

Demo: progs/series03.py

  Create a Series from dict  

  • A dict can be used as input to construct a series

    • If no index is specified:

      • the dictionary keys are used as index

    • If index is passed:

      • the values in the input data corresponding to the labels in the index will be pulled out

    Example: index is passed

    data = {'a' : 0.0, 'b' : 1.0, 'c' : 2.0}
    s = pd.Series(data, ['c', 'b', 'a', 'd'])
    
    print(s)
    
    Output:
            c    2.0
    	b    1.0
    	a    0.0
    	d    NaN

Demo: progs/series03.py

  Accessing Data from Series with Position and with Index  

  • Syntax to access the k-th element in a series by its position k:

         seriesVar[ k ]
    

  • Syntax to access an element in a series by its index index:

         seriesVar[ index ]
    

    Example:

    s1 = pd.Series([4,5,6,7,8], index = ['a','b','c','d','e'])
    
    print(s1[0])             ---> 4
    print(s1[4])             ---> 8
    
    print(s1['a'])           ---> 4
    print(s1['e'])           ---> 8
    

    Comment:   a series is similar to a Python dict... but can also use positional "index"

  Array slices with positional index  

  • Examples:

    s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
    
    print(s[1:3])          
                    b    5
    		c    6
    
    print(s[1:])
                    b    5
    		c    6
    		d    7
    		e    8
    
    print(s[:3])
                    a    4
    		b    5
    		c    6
    

DEMO: progs/series05.py

  Array slices with a list of indexes  

  • Examples:

    s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
    
    print(s[['a','c','d']])
    
    Output:
                a    4
    	    c    6
    	    d    7
    

DEMO: progs/series05.py