SERIES IN PANDAS

In this tutorial, we will learn about Series in Pandas and how to use and manipulate the elements inside it. We will learn about creating a simple series and from a dictionary while looking and working with examples.

pandas-series

What is a Series in Pandas?

Pandas series is a one dimensional data structure which can have values of integer, float and string. We use series when we want to work with a single dimensional array. It is important to note that series cannot have multiple columns. It only holds one column just like in an excel sheet. Series does have an index as an axis label. You can have your own index labels by customizing the index values.

series-in-pandas





Creating a Series in Pandas

Pandas Series can be created in different ways from MySQL table, through excel worksheet (CSV) or from an array, dictionary, list etc. Let’s look at how to create a series. Let’s import Pandas first into the python file or notebook that you are working in:

import pandas as pd

After importing pandas with an alias pd, it’s time to use the Series method:

ps = pd.Series([1,2,3,4,5])
print(ps)

Output:

0 1
1 2
2 3
3 4
4 5
dtype: int64

Changing the index of Series in Pandas

By default, the index values of your series are numbers ranging from 0 onwards. You can change the index of the series by customising the index values inside a list, in order to achieve that use the index argument to change values.

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

Output:

a 1
b 2
c 3
d 4
e 5
dtype: int64




Creating a Series from a Dictionary

Let’s learn about creating series from a dictionary, just like creating a conventional Series in Pandas, a dictionary has all the elements predefined to suit a Series. If an index is not specified while declaring the Series, then the keys are considered to be index by default. If an index is passed then keys are replaced as index labels.

import pandas as pd
import numpy as np
dict_pd = {'a' : 1, 'b' : 2, 'c' : 3, 'd': 4, 'e': 5}
series_dict = pd.Series(dict_pd)
print(series_dict)

Output:

a 1
b 2
c 3
d 4
e 5
dtype: int64

Accessing elements in Series

You can access the elements in Series by using the index position values. The index positioning will not change even though we customize the index value. However, you can always target the index label itself as well, be it any integer label or a string.

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

You can target the index label as well, let’s say that we want to pick ‘a’ as our label to test an example:

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

Output:

1




Labeling Series Column

You can set a name for your Series as well by using the ‘name’ attribute depending on the type of your data. For example:

ps = pd.Series([1,2,3,4,5], index=['a','b','c','d','e'], name="Numbers")
print(ps)

Output:

a 1
b 2
c 3
d 4
e 5
Name: Numbers, dtype: int64