14.2: The Pandas Series
The standard way to import Pandas is to use this line:
import pandas as pd
This code should go at the top of your first notebook cell, right under your
import numpy
as np
line. The two go hand in hand.
Let's begin talking about Pandas series. A Series is conceptually a set of key-value pairs. The keys are normally all of the same type (e.g., all integers or all strings), and so are the values, although the keys might be of a different type than the values (e.g., the keys could be strings and the values could be floats).
The Pandas package calls the keys “the index,” which is an overlap with the term we used for ordinary arrays.
Creating a Series
Here are a few common ways of creating a Pandas Series object in memory.
Way 1: create an empty Series
Perhaps this first one sounds dumb, but we will indeed have occasion to start off with an empty Series and then add key/value pairs to it from there. The code is simple:
Code \(\PageIndex{1}\) (Python):
my_new_series = pd.Series()
Voilà.
Way 2: pd.Series([], index=[])
As with NumPy ndarrays , we can explicitly list the values we want in a new Series . We also have to list the index values (the keys). The syntax for doing so is:
Code \(\PageIndex{2}\) (Python):
alter_egos = pd.Series(['Hulk','Spidey','Iron Man','Thor'], index=['Bruce','Peter','Tony','Thor'])
This creates the Series shown in Figure 11.1.2.
Code \(\PageIndex{4}\) (Python):
Way 3: “wrapping” an array
Associative arrays, and the Pandas Series we’ve been using to implement them, are inherently one -dimensional data structures. This is just like the NumPy arrays we used before. Pandas Series also provide a bunch of features for manipulating, querying, computing, and even graphing aspects of their content. It’s a lot of rich stuff on top of plain-old NumPy.
For this reason, it’s common to want to create a Series that just “wraps” (or encloses) an underlying NumPy ndarray , and provides all that rich stuff.
The way to do this is simple:
Code \(\PageIndex{5}\) (Python):
my_numpy_array = np.array(['Ghost','Pumpkin','Vampire','Witch'])
my_pandas_enhanced_thang = pd.Series(my_numpy_array)
You can then treat my_pandas_enhanced_thang as an ordinary aggregate variable which has the more sophisticated operations of next chapter automatically glommed on to it. The keys (index values) of this thang will simply be the integers 0 through 3.
Way 4: pd.read_csv()
Finally, there’s reading data from a text file. Data typically resides in sources and files external to our programming environment, and we want to do everything we can to play ball with this open universe.
One common data format is called CSV , which stands for comma-separated values. Files in this format are normally named with a “ .csv ” extension. As the name suggests, the lines in such a file consist of values separated by commas. For example, suppose there’s a file called disney_rides.csv whose contents looked like this:
Pirates of the Carribean,25
Small World,20
Peter Pan,29
These are the current expected wait time (in minutes) for each of these Disney World rides at some point of the day.
To read this into Python, we use the pd.read_csv() function. It’s a bit awkward since it has several mandatory arguments if you want to deal with Series . Here’s how it works:
Code \(\PageIndex{6}\) (Python):
wait_times = pd.read_csv('disney_rides.csv', index_col=0, squeeze=True, header=None)
Most of that junk is just to memorize for now, not to fully understand. If you’re curious, index_col=0 tells Pandas that the first (0th) column – namely, the ride names – should be treated as the index for the Series . The header=None means “there is no separate header row at the top of the file, Pandas, so don’t try to treat it like one.” If our .csv file did have a summary row at the top, containing labels for the two columns, then we’d skip the header=None part. Finally, “ squeeze=True ” tells Pandas, “since this is so skinny anyway – just two columns – let’s have pd.read_csv() return us a Series , rather than a more complex DataFrame object (which is the subject of a later chapter).”