How to Convert Pandas DataFrame into a List

You can convert Pandas DataFrame into a list in Python using tolist():

df.values.tolist()

In this short guide, you’ll see an example of converting Pandas DataFrame into a list.

Example of Converting Pandas DataFrame into a List

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

productprice
Tablet250
Printer100
Laptop1200
Monitor300

You then decided to capture that data in Python using Pandas DataFrame. At a certain point, you realized that you’d like to convert that Pandas DataFrame into a list.

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

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

Here is the full Python code:

import pandas as pd

data = {'product': ['Tablet', 'Printer', 'Laptop', 'Monitor'],
        'price': [250, 100, 1200, 300]
        }

df = pd.DataFrame(data)

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

And once you run the code, you’ll get the following multi-dimensional list (i.e., list of lists):

[['Tablet', 250], ['Printer', 100], ['Laptop', 1200], ['Monitor', 300]]

Optionally, you may further confirm that you got a list by adding print(type(products_list)) at the bottom of the code:

import pandas as pd

data = {'product': ['Tablet', 'Printer', 'Laptop', 'Monitor'],
        'price': [250, 100, 1200, 300]
        }

df = pd.DataFrame(data)

products_list = df.values.tolist()
print(products_list)
print(type(products_list))

As you can see, the original DataFrame was indeed converted into a list (as highlighted in yellow):

[['Tablet', 250], ['Printer', 100], ['Laptop', 1200], ['Monitor', 300]]
<class '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:

import pandas as pd

data = {'product': ['Tablet', 'Printer', 'Laptop', 'Monitor'],
        'price': [250, 100, 1200, 300]
        }

df = pd.DataFrame(data)

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

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

['Tablet', 'Printer', 'Laptop', 'Monitor']

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

In that case, simply add the following syntax:

product.append('Keyboard')

So the complete Python code would look like this:

import pandas as pd

data = {'product': ['Tablet', 'Printer', 'Laptop', 'Monitor'],
        'price': [250, 100, 1200, 300]
        }

df = pd.DataFrame(data)

product = df['product'].values.tolist()
product.append('Keyboard')

print(product)

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

['Tablet', 'Printer', 'Laptop', 'Monitor', 'Keyboard']

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 guide that explains how to convert a list to a DataFrame in Python.

Leave a Comment