How to create a series in Python Pandas is what I will be discussing in this tutorial. Welcome to the detailed tutorial on Pandas Series. If Python Pandas is your Go To library for Data Analysis then you will find yourself using Series and dataFrame more often than you can imagine. And that’s why you should learn these two data types that Pandas has introduced.
Before we go ahead with this tutorial I would suggest you to read my previous blog for the better understanding. Click here to check that out.
What is Pandas Series?
A Series is a 1D labeled homogenous data structure. Since it’s homogenous thus it can only hold the data of the same data type. In simple words, if the data is stored in a table then every single column of that table is a Series.
How To Create Pandas Series?
Creating a Pandas Series in Python Programming is very easy. To do that we just have to call the constructor of the ‘Series’ class, like this –
import pandas as pd var_1 = pd.Series()
In this code we have two statements. In the first statement we are importing the pandas library. In the same statement we have also given a nick name or an alias ‘pd’ to the same.
In the second statement, we are just calling the constructor of the class named Series. Since this class belongs to Pandas library thus we have its alias name which is PD right before the constructor name.
On the execution of this code we will have an empty series and the name of that series will be
That is how we create a Pandas Series. But for now this Series is empty and holds nothing. Now let’s learn how to add some data into this empty series.
How To Insert Data Into Pandas Series?
The best way to add data into pandas series is by using Python List. Like this
import pandas as pd var_1 = pd.Series([10, 20, 30])
On the execution of the above code we will have a Pandas Series with three integer elements added to it. You can check those elements just by printing the series like this –
import pandas as pd var_1 = pd.Series([10, 20, 30]) print(var_1)
Python List Vs Pandas Series
Things that we know about a Pandas Series so far, are –
- Series is a 1D Data Structure,
- It can hold the data of similar data type only.
Both the behaviors are very similar to an array.
But in Python we have another 1D data structure which is
Python List . However unlike Pandas Series, Python list can hold data of different data types. Now the question is, can we create a Pandas series out of Python List.
Answer to the question is Yes, it’s possible. Like this –
import pandas as pd list_01 = [1, 2, 3] series_01 = pd.Series(list_01) print(list_01) print(series_01)
Here, after importing Python Pandas I created a variable with the name
list_01. This variable is our list which contains three items. And, all these three items are of integer data type.
Since the data type of all the items of this list is the same it means we can easily add them to a Pandas Series.
In statement 3 I did exactly that. I added this Python list to our Pandas Series. I also named the series as ‘Series_01’.
How to convert Python List into Pandas Series-
Here is a detailed explanation of statement 3 –
In statement 3 on the left hand side of assignment operator we have our variable name which is
series_01 and on the right hand side of the operator we have a constructor call.
By looking at this constructor call we can say that it is a parameterized constructor of the “Series” Class.
As the parameter of this constructor we have supplied the variable which is holding our list. In our case this variable is
On execution, this call will create a Pandas series and add all the elements of the variable list_01 to it.
We can say that this is the process of converting a Python list into Pandas Series.
You can watch this detailed explanation in the video here –
See how easily we created a Pandas series from a list. But this is the case when the Python list contained the items of same data type. But, what if the data type of each of those items are different. Like this
list_01 = [196, 'Steve', 'email@example.com']
Since Series can hold data of just one data type thus the question here is – will it be possible for us to store this data into a Pandas Series. If yes, then how-
The answer to this question is Yes, we can.
When we store the data of same data type into a Pandas Series then the compiler doesn’t have to do anything. It simply takes the data and stores it into the series.
Whereas, when the data that is getting stored into a series is of different data type then the compiler has to normalize it. And compiler does that by converting the data type of the data before putting it into the Series.
That’s exactly what compiler did here. It took the data from the list and converted all the integer elements into Object and saved it into the Series.
if you are wondering what this object is. In pandas strings are stored as objects. To know why, please refer to my previous tutorial.
Type conversion completely based on compatibility. For example Integer elements can be saved as Strings but the compiler will never be able to fit the string elements into the Integer data type.
Here is a code that converts the Python list with items of different data types into Pandas series –
import pandas as pd list_01 = [196, 'Steve', 'firstname.lastname@example.org'] series_01 = pd.Series(list_01) print(list_01) print(series_01)
How To Convert The Datatype of the Series Items?
import pandas as pd list_01 = [192,193,10] series_01 = pd.Series(list_01) print(series_01)
In the above code we added three integer elements into a Series. Now let’s say for some reason you want to save this data as ‘float’ in this series. The best way of doing that is to perform the type conversion explicitly. But you don’t have to. Because this constructor ‘Series’ has a parameter using which we can perform the type conversion with the constructor call and that parameter is dtype
import pandas as pd list_01 = [192,193,10] series_01 = pd.Series(list_01, dtype=('float64')) print(series_01)
Here is the exact same code, except this time we have used ‘dtype’ parameter in the statement 3 which is the constructor call. Now on execution, everything is going to be the same. Except this time the series is going to be of float data type . That is how you perform the type conversion of the elements of a Pandas Series.
Thanks and have a great day!