To convert a pandas Series to a list in Python, the easiest way by using values.tolist() on a pandas Series.

import pandas as pd

df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"], 
                   "gender":["F","F","F","F","M","M","M","F","M"], 
                   "age":[1,2,3,4,5,6,7,8,9], 
                   "weight":[10,20,15,20,25,10,15,30,40]})

animal_types = df["animal_type"].values.tolist()

print(animal_types)

#Output:
['dog', 'cat', 'dog', 'cat', 'dog', 'dog', 'cat', 'cat', 'dog']

You can also use list() function to convert a pandas Series to a list.

import pandas as pd

df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"], 
                   "gender":["F","F","F","F","M","M","M","F","M"], 
                   "age":[1,2,3,4,5,6,7,8,9], 
                   "weight":[10,20,15,20,25,10,15,30,40]})

animal_types = list(df["animal_type"])

print(animal_types)

#Output:
['dog', 'cat', 'dog', 'cat', 'dog', 'dog', 'cat', 'cat', 'dog']

When working with collections of data, the ability to be able to easily access certain pieces of information is valuable.

One such situation is if you want to get the values of a pandas Series and create a list in Python.

There are a few ways you can convert the values of a pandas Series to a list.

Having values of a Series in a list can be useful if you want to loop over the values and perform an action.

The easiest way is to access the Series values with the pandas Series values attribute and then use the tolist() function.

Below shows a simple example of how you can convert a pandas Series to list in Python.

import pandas as pd

df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"], 
                   "gender":["F","F","F","F","M","M","M","F","M"], 
                   "age":[1,2,3,4,5,6,7,8,9], 
                   "weight":[10,20,15,20,25,10,15,30,40]})

animal_types = df["animal_type"].values.tolist()

print(animal_types)

#Output:
['dog', 'cat', 'dog', 'cat', 'dog', 'dog', 'cat', 'cat', 'dog']

Using list() to Convert pandas Series to List in Python

Another way you can convert the pandas Series values to a list is with the Python list() function.

list() tries to convert a Python object to a list. The list representation of a pandas Series is the values of the Series.

Below shows another example of how you can get the Series values of a pandas Series as a list in Python.

import pandas as pd

df = pd.DataFrame({"animal_type":["dog","cat","dog","cat","dog","dog","cat","cat","dog"], 
                   "gender":["F","F","F","F","M","M","M","F","M"], 
                   "age":[1,2,3,4,5,6,7,8,9], 
                   "weight":[10,20,15,20,25,10,15,30,40]})

animal_types = list(df["animal_type"])

print(animal_types)

#Output:
['dog', 'cat', 'dog', 'cat', 'dog', 'dog', 'cat', 'cat', 'dog']

Hopefully this article has been useful for you to be able to learn how to convert pandas Series to a list in Python.

Categorized in:

Python,

Last Update: March 22, 2024