Chapter 13. Introducing Pandas Objects
At a very basic level, Pandas objects can be thought of as enhanced
versions of NumPy structured arrays in which the rows and columns are
identified with labels rather than simple integer indices. As we will
see during the course of this chapter, Pandas provides a host of useful
tools, methods, and functionality on top of the basic data structures,
but nearly everything that follows will require an understanding of what
these structures are. Thus, before we go any further, let’s
take a look at these three fundamental Pandas data structures: the
Series, DataFrame, and Index.
We will start our code sessions with the standard NumPy and Pandas imports:
In[1]:importnumpyasnpimportpandasaspd
The Pandas Series Object
A Pandas Series is a one-dimensional array of indexed data. It can be
created from a list or array as follows:
In[2]:data=pd.Series([0.25,0.5,0.75,1.0])dataOut[2]:00.2510.5020.7531.00dtype:float64
The Series combines a sequence of values with an explicit sequence of
indices, which we can access with the values and index attributes.
The values are simply a familiar NumPy array:
In[3]:data.valuesOut[3]:array([0.25,0.5,0.75,1.])
The index is an array-like object of type pd.Index, which
we’ll discuss in more detail momentarily:
In[4]:data.indexOut[4]:RangeIndex(start=0,stop=4,step=1)
Like with a NumPy array, data can be accessed by the associated index via the familiar Python square-bracket notation: ...