# pandas: Series¶

Ha Khanh Nguyen (hknguyen)

• pandas is arguably the most famous library for Data Science in Python.
• To get started with pandas, we need to familiarize ourselves with pandas' 2 fundamental data structures:
• Series
• DataFrame

## 1. What is a Series?¶

• A Series is a one-dimensional array-like object containing a sequence of values (of similar types to NumPy types) and an associated array of data labels, called its index.
• Use the Series() function in pandas to createa an array from a list:
• Since we did not specify an index for the data, a default one consisting of the integers 0 through N - 1 (where N is the length of the data) is created.
• You can get the array representation and index object of the Series via its values and index attributes, respectively:
• We can also specify the index when we create the Series:

## 2. Indexing, Slicing and Filtering¶

### 2.1 Indexing¶

• We use index and the square brackets [] to select elements from a Series:
• In case we have labels for index, simply use the label when selecting an element:
• If you use a number index (instead of the label), Python will still understand the command and interpret it as a the row index:

### 2.2 Slicing¶

• Slicing works with numerical index:
• Slicing works with label index too, but it behaves differently than normal Python slicing in that the endpoint is inclusive:

### 2.3 Filtering¶

• Boolean filtering works with Series just like NumPy array!
• This is such an "upgrade" from the built-in list data structure:
• We can even perform a little more "complicated" checking conditions:

### 2.4 Selecting with loc and iloc¶

• These two keywords are much more interesting when we use them for DataFrame.
• In the case of Series, there is not really a reason to use them, but let's discuss them anyway:
• loc: label location
• iloc: integer location
• They do behave differently with slicing:

## 3. Arithmetic and Data Alignment¶

### 3.1 Arithmetic operations on objects with same indexes¶

• pandas Series behave very similarly to NumPy array when performing operations, that is they follow vectorization, also known as element-wise execution.
• Let's add s1 with itself:
• What's about multiplication? division? raise to a power?
• How will Python react if we add 2 Series with different lengths?

### 3.2 Arithmetic operations on objects with different indexes¶

• An important pandas feature for some applications is the behavior of arithmetic between objects with different indexes.
• When you are adding together objects, if any index pairs are not the same, the respective index in the result will be the union of the index pairs.
• For those with database experience, this is similar to an automatic outer join on the index labels.
• Let's add s1 and s2 together!
• We can also use the add() function with the fill_value argument to specify which value we want to assign to these values instead of NA:

This lecture note referenced material from Chapter 5 of Wes McKinney's Python for Data Analysis 2nd Ed.