A Practical Introduction to Pandas Series | by B. Chen | Towards Data Science

DataFrame and Series are two core data structures in Pandas. DataFrame is a 2-dimensional labeled data with rows and columns. It is like a spreadsheet or SQL table. Series is a 1-dimensional labeled array. It is sort of like a more powerful version of the Python list. Understanding Series is very important, not only because it is one of the core data structures, but also because it is the building blocks of a DataFrame.
In this article, you’ll learn the most commonly used data operations with Pandas Series and should help you get started with Pandas. The article is structured as follows:
Creating a SeriesRetrieving elementsAttributes (commonly used)Methods (commonly used)Working with Python built-in functions
Creating a Series
Retrieving elements
Attributes (commonly used)
Methods (commonly used)
Working with Python built-in functions
Please check out Notebook for the source code.
A Python list can be passed to the Pandas Series() function to create a Series.
companies = ['Google', 'Microsoft', 'Facebook', 'Apple']pd.Series(companies)0       Google1    Microsoft2     Facebook3        Appledtype: 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).
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 numberpd.Series(companies,index=[100,101,102,103])100       Google101    Microsoft102     Facebook103        Appledtype: object# Pass stringpd.Series(companies,index=['GOOGL','MSFT','FB','AAPL'])GOOGL       GoogleMSFT     MicrosoftFB        FacebookAAPL         Appledtype: object
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       Googleb    Microsoftc     Facebookd        Appledtype: 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       Googleb    Microsoftd        Appledtype: object
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    10101    10102    10103    10dtype: int64
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')
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    A1    B2    C3    DName: product, dtype: object
Please check out the following article for Pandas read_csv()
towardsdatascience.com
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    1b    2c    3dtype: int64
Retrieving the last n elements.
s[-3:]c    3d    4e    5dtype: int64
Retrieving elements within a range
s[2:4]c    3d    4dtype: int64
Retrieving elements by step
s[::2]a    1c    3e    5dtype: int64
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    1c    3d    4dtype: int64
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)
The values attribute returns an array of all the values within the series.
s.valuesarray(['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.indexRangeIndex(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_uniqueTrue
The dtype attribute returns the data type. It gives us 'O' which is short for object.
s.dtypedtype('O')
The size attribute returns the number of items in a Series.
s.size4
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.ndim1
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.
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.01     5.02     3.03     2.54     8.0dtype: float64s.head(2)0    10.01     5.0dtype: float64s.tail(2)4     8.05    11.0dtype: float64
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.583333sum           39.500000product    33000.000000dtype: float64
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    3b    2dtype: int64
The sort_values() method sorts a Series in ascending or descending order by some criterion.
# ascending by defaults.sort_values()# To sort it in descenting orders.sort_values(ascending=False)# To modify the original seriess.sort_values(inplace=True)
The sort_index() method sorts a Series by index label. It is similar to sort_value().
# ascending by defaults.sort_index()# To sort it in descenting orders.sort_index(ascending=False)# To modify the original seriess.sort_index(inplace=True)
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    False1    False2    False3     True4     Truedtype: 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
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
ge(): greater than or equal
eq(): equal
le(): less than or equal
lt(): 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
len() and type() are Python built-in functions for size and data type.
len(s)6type(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 sFalse
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.valuesTrue
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.
Using Pandas method chaining to improve code readability
How to do a Custom Sort on Pandas DataFrame
When to use Pandas transform() function
Pandas concat() tricks you should know
Difference between apply() and transform() in Pandas
All the Pandas merge() you should know
Working with datetime in Pandas DataFrame
Pandas read_csv() tricks you should know
4 tricks you should know to parse date columns with Pandas read_csv()
More tutorials can be found on my Github