Pandas To List

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

Pandas To List
  1. Df.count function in pandas is used to get the count of values of all the columns at once. So the resultant value will be. Count the value of single columns in pandas: Method 1. In the below example we will get the count of value of single specific column in pandas python dataframe.
  2. Convert a Pandas row to a list Now we would like to extract one of the dataframe rows into a list. For simplicity let’s just take the first row of our Pandas table. #Python3 firstrow = list (data.loc).

The pandas object data type is commonly used to store strings. However, you can not assume that the data types in a column of pandas objects will all be strings. This can be especially confusing when loading messy currency data that might include numeric values with symbols as well as integers and floats.

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

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

Pandas To List

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,
Output
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,
Output:
It returns a Series object names, and we have confirmed that by printing its type.

Convert Pandas To List

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.
Output:
This is how we converted a dataframe column into a list.

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,
Output
We converted the column ‘Name’ into a list in a single line. Let’s see what happened inside it,

How did it work?

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
Output:
Names is a numpy array, and we confirmed it by printing its types.

Pandas To List

Step 3: Convert a Numpy array into a list

Numpy array provides a function tolist() to convert its contents to a list,
Output:
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,
Output:

Related Posts:

Contents

Pandas Tutorial – Pandas Examples

pandas library helps you to carry out your entire data analysis workflow in Python.

With Pandas, the environment for doing data analysis in Python excels in performance, productivity, and the ability to collaborate.

Import pandas

pandas is built on numpy. So, while importing pandas, import numpy as well.

This is how the pandas community usually import and alias the libraries. We will also use the same alias names in our pandas examples going forward.

Following is a list of Python Pandas topics, we are going to learn in these series of tutorials.

Pandas to list

Pandas Series Basics

Pandas DataFrame Basics

Cell Operations

Pandas DataFrame Column Operations

Pandas DataFrame Row Operations

Pandas Conversions

Convert List To Pandas Df

Summary

Pandas To List Of Strings

In this tutorial of Python Examples, we learned about Python Pandas, and different concepts of Python Pandas that can be used in your Python application.