• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Convert pandas Series to List in Python

Convert pandas Series to List in Python

October 14, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Python Add Months to Datetime Variable Using relativedelta() Function
  • 2.  Python power function – Exponentiate Numbers with math.pow()
  • 3.  Using Lambda Expression with max() in Python
  • 4.  Add Seconds to Datetime Variable Using Python timedelta() Function
  • 5.  Python Add Days to Date Using datetime timedelta() Function
  • 6.  How to Serialize a Model Object with a List of Lists Using Django Rest Framework in Python
  • 7.  Using Python to Check if Deque is Empty
  • 8.  Python Check if Object Has Attribute
  • 9.  Using Python to Replace Multiple Spaces with One Space in String
  • 10.  Python Get Yesterday’s Date

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x