Convert List to Series & Vice Versa in Python (3 Examples)

 

Hi! This tutorial will demonstrate 2 simple methods for turning a list into a pandas series and one method for turning the series back into a list in the Python programming language.

But before we get into the tutorial proper, here is a quick overview:

Let’s dive into Python code!

 

Create Sample List

Here, we will create a sample list that we will turn into pandas series. Therefore, in your preferred Python IDE, run the line of code below to create the sample list.

my_list = ["Toyota", "Mercedes", "Ford", "Jeep", "Honda"]

We created a sample list called my_list, which contains 5 string items. Let’s import the pandas library next to convert it to a pandas series!

 

Install & Import pandas Library

We need to install and import the Python pandas library, which is the go-to library in Python for data manipulation and analysis. Run the lines of code below to install and import pandas.

# install pandas
pip install pandas
 
# import pandas
import pandas as pd

With the pandas library installed and imported into our Python programming environment, we now have access to all of its functions, including the ones we will make use of in this tutorial.
 

Example 1: Turn List to Series Using Series() Function

In this first example, we will convert the list to a series with the pandas Series() function.

series = pd.Series(my_list)
print(series)
 
 
#0      Toyota
#1    Mercedes
#2        Ford
#3        Jeep
#4       Honda
#dtype: object

We can confirm that the object is indeed a series by running the following code.

print(type(series))
 
 
# <class 'pandas.core.series.Series'>

Looks like it worked!

 

Example 2: Turn List to Series Using Series() Function with Index Argument

In this second example, we will turn the list into a series using the Series() function, with the index argument this time.

series = pd.Series(my_list, index = ["A", "B", "C", "D", "E"])
print(series)
 
#A      Toyota
#B    Mercedes
#C        Ford
#D        Jeep
#E       Honda
#dtype: object
 
 
print(type(series))
# <class 'pandas.core.series.Series'>

By default, the series is created with index numbers, starting from 0, as we saw in the first example. However, as demonstrated above, we can parse an array of values that we want to use as the index of series through the index = argument.

 

Example 3: Turn Series back into List Using tolist() Method

Having demonstrated 2 methods of turning the list into a series, we will now demonstrate how to reverse the process by turning the series back into a list using the tolist() method. Therefore, run the code below to do so:

my_list = series.tolist()
print(my_list)
 
 
# ['Toyota', 'Mercedes', 'Ford', 'Jeep', 'Honda']

We can verify that it is indeed a list object by running:

print(type(my_list))
 
# <class 'list'>

Well done!

So this tutorial has shown 2 simple ways of converting a list into a series and one simple way of converting the series back to a list in the Python programming language. I hope you found it helpful, and I will see you at the next one!
 

Video, Further Resources & Summary

Do you need more explanations on how to convert a list into a series and vice versa in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to convert a list into a series and vice versa in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to convert a list into a series and vice versa in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top