A Practical Introduction to Pandas Series
By: B. Chen
Tags: Data Science, Pandas, Series, Data Analysis, Python

1. Creating a Series

1.1 from a Python list

A Python list can be passed to the Pandas Series() function to create a Series.

companies = ['Google', 'Microsoft', 'Facebook', 'Apple'] pd.Series(companies) 0 Google

1 Microsoft

2 Facebook

3 Apple

dtype: object

Notes:

All values are represented in the exact same order as they appeared in the original Python list.

The dtype says object (it is the internal Pandas lingo for the string ).

says (it is the internal Pandas lingo for the ). There is an additional column called Index. In this case, it resembles an index from a Python list. But one of the key advantages of a Pandas Series is that the index labels do not have to be numeric, they can be any data type.

We can use the argument index to specify a custom index:

# Pass number

pd.Series(companies,index=[100,101,102,103]) 100 Google

101 Microsoft

102 Facebook

103 Apple

dtype: object

# Pass string

pd.Series(companies,index=['GOOGL','MSFT','FB','AAPL'])

GOOGL Google

MSFT Microsoft

FB Facebook

AAPL Apple

dtype: object

1.2 from a dictionary

A dictionary can be passed to the Pandas Series() function. If the index is not specified, the dictionary keys are taken in a sorted order to construct the index.

companies = {

'a': 'Google',

'b': 'Microsoft',

'c': 'Facebook',

'd': 'Apple'

}

pd.Series(companies) a Google

b Microsoft

c Facebook

d Apple

dtype: object

If the index is specified, then the values in data corresponding to the labels in the index will be pulled out.

pd.Series(

companies,

index=['a', 'b', 'd']

) a Google

b Microsoft

d Apple

dtype: object

1.3 from a Scalar

If the input is a scalar value, then the argument index must be provided. The value will be repeated to match the length of the index :

pd.Series(10, index=[100, 101, 102, 103]) 100 10

101 10

102 10

103 10

dtype: int64

1.4 from the Pandas read_csv() function

By default, the Pandas read_csv() function will import data as a DataFrame. It loads the entire dataset into memory, and this could be a memory and performance issue when importing a huge CSV file.

pd.read_csv('data.csv')

Result of pd.read_csv(‘data.csv’) (Image by author)

If we want the data to be imported into a Series instead of a DataFrame, we can provide additional arguments usecols and squeeze . The squeeze=True will convert a DataFrame of one column into a Series.

pd.read_csv('data.csv', usecols=['product'], squeeze=True) 0 A

1 B

2 C

3 D

Name: product, dtype: object

Please check out the following article for Pandas read_csv()

2. Retrieving elements

2.1 with position

Pass the index to retrieve an element, for example, the first element.

s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) s[0]

1

Retrieving the first n elements, for example, the first 3 elements

s[:3] a 1

b 2

c 3

dtype: int64

Retrieving the last n elements.

s[-3:] c 3

d 4

e 5

dtype: int64

Retrieving elements within a range

s[2:4] c 3

d 4

dtype: int64

Retrieving elements by step

s[::2] a 1

c 3

e 5

dtype: int64

2.2 with index label

Pandas Series is a 1-dimensional labeled array that we can access elements by index label.

Retrieving a single element using an index label.

s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) s['a']

1

Retrieving multiple elements using a list of index labels.

s[['a','c','d']] a 1

c 3

d 4

dtype: int64

3. Attributes

Objects in Python have Attributes and Methods. Attributes are a way that we can use to find information without manipulating or destroying anything. Methods actually do something to the object. It may be manipulating it, or adding value, or do some calculation with the object’s values.

A Pandas Series is just one type of Python objects. In this section, we will cover some of the commonly used attributes in the Pandas Series.

Let’s first create a Pandas Series.

companies = ['Google', 'Microsoft', 'Facebook', 'Apple']

s = pd.Series(companies)

3.1 Values and indexes

The values attribute returns an array of all the values within the series.

s.values array(['Google', 'Microsoft', 'Facebook', 'Apple'], dtype=object)

The index attribute returns a RangeIndex object. We can see it starts at 0 and stops at 4. The last part is called step and that’s telling us that it’s incrementing by 1

s.index RangeIndex(start=0, stop=4, step=1)

The is_unique attribute returns a boolean ( True or False ). It is a really convenient way to check if every series value is unique or not.

s.is_unique True

3.2 Data type and size

The dtype attribute returns the data type. It gives us 'O' which is short for object.

s.dtype dtype('O')

The size attribute returns the number of items in a Series.

s.size 4

The shape attribute returns the number of rows by the number of columns in a tuple

s.shape (4,)

We also have the ndim attribute which is short for the number of dimensions and a Series is always a 1-dimensional object

s.ndim 1

4. Methods

A method as mentioned actually does something to the object. It may be manipulating it, or adding value, or do some calculation with the object’s values.

4.1 Showing rows

The head() and tail() methods return the top and last n rows respectively. n defaults to 5 if you don’t give any value. They are useful for quickly verify data, for example after sorting or appending rows.

prices = [10, 5, 3, 2.5, 8, 11]

s = pd.Series(prices) s.head() 0 10.0

1 5.0

2 3.0

3 2.5

4 8.0

dtype: float64 s.head(2) 0 10.0

1 5.0

dtype: float64 s.tail(2)

4 8.0

5 11.0

dtype: float64

4.2 Performing aggregations

We can perform aggregation on a Series, such as mean() , sum() , product() , max() , min() , and median()

s.mean() s.sum() s.product()

If we need multiple aggregations, we can pass them in a list to agg() method

s.agg(['mean','sum','product']) mean 6.583333

sum 39.500000

product 33000.000000

dtype: float64

4.3 Counting values

The unique() and nunique() methods return the unique values and the number of unique values, respectively.

s = pd.Series(['a','b','b','a','a']) s.unique()

array(['a', 'b'], dtype=object) s.nunique()

2

The value_counts() method returns the number of occurrences of each unique value in a Series. It is useful to get an overview of the distribution of values.

s.value_counts() a 3

b 2

dtype: int64

4.4 Sorting by values or index labels

The sort_values() method sorts a Series in ascending or descending order by some criterion.

# ascending by default

s.sort_values() # To sort it in descenting order

s.sort_values(ascending=False) # To modify the original series

s.sort_values(inplace=True)

The sort_index() method sorts a Series by index label. It is similar to sort_value() .

# ascending by default

s.sort_index() # To sort it in descenting order

s.sort_index(ascending=False) # To modify the original series

s.sort_index(inplace=True)

4.5 Working with missing values

The isna() method returns a boolean same-sized object indicating if the values are missing.

s = pd.Series([1, 2, 3, np.nan, np.nan])

s.isna() 0 False

1 False

2 False

3 True

4 True

dtype: bool

We can count the number of missing values by chaining the result with the sum() method.

s.isna().sum()

2

The count() method returns the number of non-missing values in a Series.

s.count()

3

4.6 Searching values

The nlargest() and nsmallest() methods return the largest and smallest values in a Series. By default, it is showing 5 results if you don’t give any value.

s.nlargest() s.nlargest(2)

There are logical operator methods:

gt() : greater than

: greater than ge() : greater than or equal

: greater than or equal eq() : equal

: equal le() : less than or equal

: less than or equal lt() : less than

: less than ne() : not equal

They are equivalent to > , >= , = , <= , < and != respectively, but with support to substitute a fill_value for missing values.

s.le(5, fill_value=0)

s <= 5

5. Working with Python built-in functions

len() and type() are Python built-in functions for size and data type.

len(s)

6 type(s)

pandas.core.series.Series

dir() short for the directory. If we pass a Series onto it, it is going to give us an output of all of the available attributes and methods.

dir(s) ['T',

'_AXIS_LEN',

'_AXIS_NAMES',

'_AXIS_NUMBERS',

'_AXIS_ORDERS',

'_AXIS_REVERSED',

'_AXIS_TO_AXIS_NUMBER',

'_HANDLED_TYPES',

'__abs__',

....]

To condense a Series, we can use the built-in list() function. It’s kind of doing the exact opposite operation that we passed a list to Series() .

list(s) [10.0, 5.0, 3.0, 2.5, 8.0, 11.0]

Similarly, dict() is a reverse of the operations when we passed a dictionary to Series()

dict(s) {0: 10.0, 1: 5.0, 2: 3.0, 3: 2.5, 4: 8.0, 5: 11.0}

Python in keyword returns a boolean value that compares the value you provide to the values in the list. It’s going to return True if it exists among those values and False if it does not.

2.5 in s False

2.5 in s returns False because by default Pandas is going to look among the index labels not the actual values within the Series. Just make sure to add the extra values attribute:

2.5 in s.values True

Conclusion

Pandas Series is a core data structure in Pandas and the basic building blocks for a DataFrame. This article has covered the commonly used data operations with Pandas Series through 5 aspects.

I hope this article will help you to save time in learning Pandas. There are, of course, more attributes and methods to be used with Pandas Series. I recommend you to check out the Series() API documentation and to know about other things you can do.

Thanks for reading. Please check out the notebook for the source code and stay tuned if you are interested in the practical aspect of machine learning.

You may be interested in some of my other Pandas articles:

More tutorials can be found on my Github