Numpy To List


In this article, we will discuss different ways to convert a dataframe column into a list.

We can use numpy ndarray tolist function to convert the array to a list. If the array is multi-dimensional, a nested list is returned. For one-dimensional array, a list with the array elements is returned. Table of Contents hide. My Dashboard; IST Advanced Topics Primer; Pages; Python Lists vs. Numpy Arrays - What is the difference?

Fits of all, create a dataframe object that we are going to use in this example,
Now how to fetch a single column out of this dataframe and convert it to a python list?

Numpy array to list

There are different ways to do that, lets discuss them one by one.

Convert a Dataframe column into a list using Series.to_list()

To turn the column ‘Name’ from the dataframe object student_df to a list in a single line,
What did happen here?

How did it work?

Let’s break down the above line into steps,

Step 1: Fetch a column as series

Select the column ‘Name’ from the dataframe using [] operator,
It returns a Series object names, and we have confirmed that by printing its type.

Step 2 : Convert the Series object to the list

Series class provides a function Series.to_list(), which returns the contents of Series object as list. Use that to convert series names into a list i.e.
This is how we converted a dataframe column into a list.

Important Note:

It might be possible that it gives you an error i.e.
If you get that error, then please check your Pandas version, you may be using pandas version less than 24.
Upgrade you pandas to the latest version using the following command,

Convert a Dataframe column into a list using numpy.ndarray.tolist()

Another way is converting a Dataframe column into a list is,
We converted the column ‘Name’ into a list in a single line. Let’s see what happened inside it,

How did it work?

Turn Numpy Array To List

Let’s break down the above line into steps,

Step 1: Select a column as a Series object

Select the column ‘Name’ from the dataframe using [] operator,
It returns a Series object.

Step 2: Get a Numpy array from a series object using Series.Values
Names is a numpy array, and we confirmed it by printing its types.

Step 3: Convert a Numpy array into a list

Numpy array provides a function tolist() to convert its contents to a list,
This is how we selected our column ‘Name’ from Dataframe as a Numpy array and then turned it to a list.

The complete example is as follows,

Related Posts:

What is NumPy?

NumPy is a Python library used for working with arrays.

Numpy To List Python

It also has functions for working in domain of linear algebra, fourier transform, and matrices.

NumPy was created in 2005 by Travis Oliphant. It is an open source project and you can use it freely.

Numpy To List

NumPy stands for Numerical Python.

Why Use NumPy?

In Python we have lists that serve the purpose of arrays, but they are slow to process.

Numpy To List Along Axis

Numpy tolist

NumPy aims to provide an array object that is up to 50x faster than traditional Python lists.

The array object in NumPy is called ndarray, it provides a lot of supporting functions that make working with ndarray very easy.

Arrays are very frequently used in data science, where speed and resources are very important.

Data Science: is a branch of computer science where we study how to store, use and analyze data for deriving information from it.

Why is NumPy Faster Than Lists?

NumPy arrays are stored at one continuous place in memory unlike lists, so processes can access and manipulate them very efficiently.

This behavior is called locality of reference in computer science.

This is the main reason why NumPy is faster than lists. Also it is optimized to work with latest CPU architectures.

Which Language is NumPy written in?

NumPy is a Python library and is written partially in Python, but most of the parts that require fast computation are written in C or C++.

Where is the NumPy Codebase?

Numpy Matrix To List

The source code for NumPy is located at this github repository

github: enables many people to work on the same codebase.