Pandas Series with Python

The Pandas series is a one-dimensional object which contains an array of data and an array of data labels called its index. In this article, I will introduce you to a tutorial on Pandas series with Python.

What is the Series in Pandas?

A Pandas series is used to model one-dimensional data, similar to a list in Python. The Series also has some extra bits of data which includes an index and a name. A common idea across pandas is the notion of the axis. Since a series is one-dimensional, it has a single axis called an index.

Also, Read – 100+ Machine Learning Projects Solved and Explained.

For example, below is a table of the number of songs composed by an artist:

ArtistData
0145
1142
2130
3100

To represent this data in pure Python, you can use a data structure similar to the following: it is a dictionary containing a list of data points, stored under the key ‘data’:

ser = {'index':[0, 1, 2, 3],
'data':[145, 142, 130, 100]}
print(ser)

{‘index’: [0, 1, 2, 3], ‘data’: [145, 142, 130, 100]}

Pandas Series with Python

With the above table in mind, let’s see how to create a series in pandas with Python. It’s easy to create a Series object from a list:

import pandas as pd
songs = pd.Series([145, 142, 130, 100], name='counts')
print(songs)
0  145
1 142
2 130
3 100
Name: counts, dtype: int64

When the interpreter prints our series, the pandas do their best to format it for the current size of the terminal. The leftmost column is the index column that contains entries for the index. The generic name of an index is an axis and the values of the index; 0, 1, 2, 3 which are called axis labels.

The index of a Pandas Series can be a string also, in that case, Pandas represents it as an object and not a string:

songs = pd.Series([145, 142, 130, 100], name='counts', 
                  index=['Paul', 'John', 'George', 'Ringo'])
print(songs)
Paul      145
John      142
George    130
Ringo     100
Name: counts, dtype: int64

I hope you liked this article on a tutorial on Pandas Series with Python. Feel free to ask your valuable questions in the comments section below.

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1435

Leave a Reply