How to convert pandas DataFrame into a List

At times, you may need to convert pandas DataFrame into a list in Python.

But how would you do that?

To accomplish this task, you can use tolist as follows:

df.values.tolist()

In this short post, I’ll show you an example of using tolist to convert pandas DataFrame into a list.

Example of using tolist to convert pandas DataFrame into a list

Let’s say that you have the following data about products and prices:

 

ProductPrice
Tablet250
iPhone800
Laptop1200
Monitor300

 

You then decided to capture that data in Python using pandas DataFrame.

At a certain point, you realize that you’d like to convert that pandas DataFrame into a list.

To accomplish this goal, you may use the following Python code that will allow you to convert the DataFrame into a list, where:

  • The top part of the code, contains the syntax to create the DataFrame with our data about products and prices
  • The bottom part of the code converts the DataFrame into a list using:
    df.values.tolist()

Here is the full Python code:

 

from pandas import DataFrame

Products = {'Product': ['Tablet','iPhone','Laptop','Monitor'],
            'Price': [250,800,1200,300]
            }

df = DataFrame(Products, columns= ['Product', 'Price'])

Products_list = df.values.tolist()
print (Products_list)

 

And once you run the code, you’ll get:

 

How to convert pandas DataFrame into a List

 

But wait a minute… What about the column names?

If you want to add the column names into your list, you’ll need to modify the code as follows:

 

from pandas import DataFrame

Products = {'Product': ['Tablet','iPhone','Laptop','Monitor'],
            'Price': [250,800,1200,300]
            }

df = DataFrame(Products, columns= ['Product', 'Price'])

Products_list = [df.columns.values.tolist()] + df.values.tolist()
print (Products_list)

 

This is what you’ll see once running the code:

 

Convert pandas DataFrame into a List

 

Honestly, between you and me, this list looks ugly.

Let’s do some formatting to display the list in a tabular form:

 

from pandas import DataFrame

Products = {'Product': ['Tablet','iPhone','Laptop','Monitor'],
            'Price': [250,800,1200,300]
            }

df = DataFrame(Products, columns= ['Product', 'Price'])

Products_list = [df.columns.values.tolist()] + df.values.tolist()
f = '{:<8}|{:<15}' # formatting

for i in Products_list:
    print(f.format(*i))

 

And the result:

tolist Python

Much better!

So once you have your list nicely formatted, you may perform some additional actions, such as appending values to the list.

For example, let’s say that you have another product (e.g., a Printer for a price of $150) and you want to append it to the list.

Here is the Python code that you may use:

 

from pandas import DataFrame

Products = {'Product': ['Tablet','iPhone','Laptop','Monitor'],
            'Price': [250,800,1200,300]
            }

df = DataFrame(Products, columns= ['Product', 'Price'])

Products_list = [df.columns.values.tolist()] + df.values.tolist()
Products_list.append(['Printer',150])
f = '{:<8}|{:<15}' # format


for i in Products_list:
    print(f.format(*i))

 

The Printer, with a price of $150, would now get appended to the list:

How to convert pandas DataFrame into a List