pandas series to a list transition

Convert Pandas Series to a List

Lists are very flexible to work with in python and it may happen depending upon the use-case that you’d want to work with a list instead of a series. In this tutorial, we will look at how to convert a pandas series to a list.

There are a number of ways to get a list from a pandas series. You can use the tolist() function associated with the pandas series or pass the series to the python built-in list() function. The following is the syntax to use the above functions:

# using tolist()
ls = s.tolist()
# using list()
ls = list(s)

Here, s is the pandas series you want to convert. Both the functions return a list with the series values.

Let’s look at some examples of using the above methods to create a list from a series. First, we’ll create a sample pandas series which we will be using throughout this tutorial.

import pandas as pd

# pandas series Wimbledon winners from 2015 to 2019
wimbledon_winners = pd.Series(index=[2015, 2016, 2017, 2018, 2019],
                   data=['Novak Djokovic', 'Andy Murray', 'Roger Federer', 'Novak Djokovic', 'Novak Djokovic'],
                   name='Name')

# display the series
print(wimbledon_winners)

Output:

2015    Novak Djokovic
2016       Andy Murray
2017     Roger Federer
2018    Novak Djokovic
2019    Novak Djokovic
Name: Name, dtype: object

You can see the contents of the series object above. Let’s confirm the type of the object.

# check the type
print(type(wimbledon_winners))

Output:

<class 'pandas.core.series.Series'>

We now have a pandas series containing the name of Wimbledon Winners from 2015 to 2019 with the year as its index.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

First, let’s see the usage of the pandas series tolist() function to get a list from a series.

ls = wimbledon_winners.tolist()
# check the type
print(type(ls))
# print the content
print(ls)

Output:

<class 'list'>
['Novak Djokovic', 'Andy Murray', 'Roger Federer', 'Novak Djokovic', 'Novak Djokovic']

You can see the resulting list with all the series values. Note that the tolist() function does not modify the series in-place rather it returns a list of the series values.

For more on the pandas series tolist() function, refer to its documentation.

Alternatively, you can use the python built-in list() function to create a list from a pandas series. Let’s create a list from the “wimbledon_winners” series using this function.

ls = list(wimbledon_winners)
# check the type
print(type(ls))
# print the content
print(ls)

Output:

<class 'list'>
['Novak Djokovic', 'Andy Murray', 'Roger Federer', 'Novak Djokovic', 'Novak Djokovic']

You can see that the resulting list has all the values from the series “wimbledon_winners”.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Tutorials on pandas series –

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top