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 guide, I’ll show you an example of using tolist to convert pandas DataFrame into a list. I’ll also review the steps to convert an individual column in the 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, which 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 you run 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

Convert an Individual Column in the DataFrame into a List

Let’s say that you’d like to convert the ‘Product’ column into a list.

You can then use the following template in order to convert an individual column in the DataFrame into a list:

df['column name'].values.tolist()

Here is the complete Python code to convert the ‘Product’ column into a list:

from pandas import DataFrame

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

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

Product = df['Product'].values.tolist()
print (Product)

Run the code, and you’ll get the following list:

list in Python

What if you want to append an additional item (e.g., Printer) into the ‘Product’ list?

In that case, simply add the following syntax:

Product.append('Printer')

So the full Python code would look like this:

from pandas import DataFrame

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

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

Product = df['Product'].values.tolist()
Product.append('Printer')

print (Product)

You’ll now see the ‘Printer’ at the end of the list:

tolist

An Opposite Scenario

Sometimes, you may face an opposite situation, where you’ll need to convert a list to a DataFrame. If that’s the case, you may want to check the following source that explains how to convert a list to a DataFrame in Python.