Creating Series objects
In the following section, we will see how to create a series of objects.
Getting ready
Before trying this recipe, import the numpy
and pandas
modules with the following code:
import numpy as np import pandas as pd
How to do it...
Run the following code in a code cell; this will create three objects of Series
type:
series1 = pd.Series(['Alice', 'Bob', 'Joe', 'Mary']) series2 = pd.Series(['Honda', 'Honda', 'Honda', 'Toyota', 'Ford'], index=['CR-V', 'Civic', 'Accord', 'Highlander', 'F-150']) series3 = pd.Series({'a':102, 'b':np.NaN, 'c':303}, dtype=np.float64)
After the objects are created, their contents can be checked by entering the variable that represents each object in a code cell by itself.
How it works...
The simplest constructor for a Series
object takes as argument a Python iterable, as shown in the first example, reproduced as follows:
series1 = pd.Series(['Alice', 'Bob', 'Joe', 'Mary']) series1
The iterable must be one-dimensional, such as a plain...