Python Pandas Series: A Quick Guide

Python

If you’re like me, then you love learning new things. Python is a great language for doing just that. And Pandas is a great library for working with data in Python.

In this quick guide, I’m going to show you how to work with Pandas Series in Python. We’ll cover the basics of what a Series is, how to create one, and how to manipulate and access data within a Series.

So let’s get started!

What is a series?

The Pandas Series is a one-dimensional array that stores data of various data types. We can access the contents of a series by their indices in the same way as accessing data of an array. The two most important key features of the pandas series are given as follows.

  • A Series can’t contain multiple columns. So it’s one-dimensional.
  • We can easily convert the list, tuple, and dictionary into series using the series() method.

A pandas Series can be created using the command below−

pandas.Series( data, index, dtype )

The parameters in the above python command are:

  • data – It can be of various forms like an array, list, or constants.
  • Index – This value is unique and hashable, the same length as the data. By default, it starts from 0 and so on according to respective contents, if no index is passed.
  • dtype – It is the datatype of our input content. If it is not passed, it was inferred.

Let us create our Pandas series

Now we are going to create our series in different ways followed by examples below.

Creating a Series from ndarray

Example 1

#We need to import both the pandas and numpy libraries to create a series
import pandas as pd
import numpy as np

data = np.array(['h','e','l','l','o'])
our_series = pd.Series(data)
#our series created named our_series

We can see in the above code snippet that We just created our first python series named our_series. It will create our series with indices starting from 0 and so on. Let’s print this and see how it was created.

print (our_series)
0   h
1   e
2   l
3   l
4   o
dtype: object

Example 2

Let us create another using the same method but in this example, we will pass some of our manual indices. Let’s get into our code snippet.

#We need to import both the pandas and numpy libraries to create a series
import pandas as pd
import numpy as np

data = np.array(['h','e','l','l','o'])
our_series = pd.Series(data, index=[100,101,102,103,104])
#our series created named our_series

Let’s see the output of our code snippet by printing the series created.

print (our_series)
100   h
101   e
102   l
103   l
104   o
dtype: object

Here we can see that the indexes are the same as the values that we have passed while creating the series.

Creating a Series from a Dictionary

Example 1

#importing the pandas and numpy library 
import pandas as pd
import numpy as np

data = {'a' : 0., 'b' : 1., 'c' : 2.}
our_series = pd.Series(data)
print (our_series)

A dictionary can be passed as an input and if no index is specified, then the dictionary keys are taken in a sorted order to construct the index. If the index is passed, the corresponding data as the index will be preferred as follows.

print(our_series)
a 0.0
b 1.0
c 2.0
dtype: float64

Example 2

#importing the pandas and numpy library 
import pandas as pd
import numpy as np

data = {'a' : 0., 'b' : 1., 'c' : 2.}
our_series = pd.Series(data, index=['b','c','a'])

Here we passed manual indices corresponding to the keys of our dictionary. By printing the created series we can get the output as follows.

print (our_series)
b 1.0
c 2.0
a 0.0
dtype: float64

Creating a Series from Scalar

We are going to input data in a scalar value, an index must be provided. The value will be repeated to match the length of the index. we are going to use pandas.Series() . Let’s follow the code snippet below.

#import the pandas library and numpy library
import pandas as pd
import numpy as np

ssss = pd.Series(5, index=[0, 1, 2, 3])

We can see the resultant series by printing our newly created series.

print(ssss)
0  5
1  5
2  5
3  5
dtype: int64

Creating an empty Series

#importing only pandas library as pd
import pandas as pd

s = pd.Series()

If we print the above series, We can get our empty series as follows.

print (s)
Series([], dtype: float64)

We will learn how to access the data of series using respective indices

Here We are going to retrieve the first element of our series. As we already know, index counting starts from zero, which means the first element is stored at the zeroth position and so on.

import pandas as pd
sss = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
#our series created

#retrieving the first element using his index
print (s['a'])

We will get the output as follows.

1

In this way, if we print for any other index, We can get as follows.

print (s[['a','c','d']])
a  1
c  3
d  4
dtype: int64

print (sss[0])
1

print (sss[4])
5

#retieving first three elements
print (sss[:3])
a  1
b  2
c  3
dtype: int64

#retrieving the last three elements
print(sss[-3:])
c  3
d  4
e  5
dtype: int64

Conclusion

In this quick guide, we’ve learned about Python Pandas Series. We’ve seen how to create a Series, how to manipulate and access data within a Series, and how to perform some basic operations on a Series. I hope you’ve found this guide helpful. If you have any questions, feel free to post them in the comments below.