7.14.1 pandas Series

  • An enhanced one-dimensional array
  • Supports custom indexing, including even non-integer indices like strings
  • Offers additional capabilities that make them more convenient for many data-science oriented tasks
    • Series may have missing data
    • Many Series operations ignore missing data by default

Creating a Series with Default Indices

  • By default, a Series has integer indices numbered sequentially from 0
In [1]:
import pandas as pd
In [2]:
grades = pd.Series([87, 100, 94])

Creating a Series with All Elements Having the Same Value

  • Second argument is a one-dimensional iterable object (such as a list, an array or a range) containing the Series’ indices
  • Number of indices determines the number of elements
In [149]:
pd.Series(98.6, range(3))
Out[149]:
0    98.6
1    98.6
2    98.6
dtype: float64

Accessing a Series’ Elements

In [150]:
grades[0]
Out[150]:
87

Producing Descriptive Statistics for a Series

  • Series provides many methods for common tasks including producing various descriptive statistics
  • Each of these is a functional-style reduction
In [151]:
grades.count()
Out[151]:
3
In [152]:
grades.mean()
Out[152]:
93.66666666666667
In [153]:
grades.min()
Out[153]:
87
In [154]:
grades.max()
Out[154]:
100
In [155]:
grades.std()
Out[155]:
6.506407098647712
  • Series method describe produces all these stats and more
  • The 25%, 50% and 75% are quartiles:
    • 50% represents the median of the sorted values.
    • 25% represents the median of the first half of the sorted values.
    • 75% represents the median of the second half of the sorted values.
  • For the quartiles, if there are two middle elements, then their average is that quartile’s median
In [156]:
grades.describe()
Out[156]:
count      3.000000
mean      93.666667
std        6.506407
min       87.000000
25%       90.500000
50%       94.000000
75%       97.000000
max      100.000000
dtype: float64

Creating a Series with Custom Indices

Can specify custom indices with the index keyword argument

In [157]:
grades = pd.Series([87, 100, 94], index=['Wally', 'Eva', 'Sam'])
In [158]:
grades
Out[158]:
Wally     87
Eva      100
Sam       94
dtype: int64

Dictionary Initializers

  • If you initialize a Series with a dictionary, its keys are the indices, and its values become the Series’ element values
In [159]:
grades = pd.Series({'Wally': 87, 'Eva': 100, 'Sam': 94})
In [160]:
grades
Out[160]:
Wally     87
Eva      100
Sam       94
dtype: int64

Accessing Elements of a Series Via Custom Indices

  • Can access individual elements via square brackets containing a custom index value
In [161]:
grades['Eva']
Out[161]:
100
  • If custom indices are strings that could represent valid Python identifiers, pandas automatically adds them to the Series as attributes
In [162]:
grades.Wally
Out[162]:
87
  • dtype attribute returns the underlying array’s element type
In [163]:
grades.dtype
Out[163]:
dtype('int64')
  • values attribute returns the underlying array
In [164]:
grades.values
Out[164]:
array([ 87, 100,  94])

Creating a Series of Strings

  • In a Series of strings, you can use str attribute to call string methods on the elements
In [165]:
hardware = pd.Series(['Hammer', 'Saw', 'Wrench'])
In [166]:
hardware
Out[166]:
0    Hammer
1       Saw
2    Wrench
dtype: object
In [167]:
hardware.str.contains('a')
Out[167]:
0     True
1     True
2    False
dtype: bool
  • Use string method upper to produce a new Series containing the uppercase versions of each element in hardware
In [168]:
hardware.str.upper()
Out[168]:
0    HAMMER
1       SAW
2    WRENCH
dtype: object

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.