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, you’ll see 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:
product | price |
Tablet | 250 |
Printer | 100 |
Laptop | 1200 |
Monitor | 300 |
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 in order 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:
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]]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:
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.
# Add a list to data.frame in single 'element' slot # 1] Make data.frame from named list [names are optional] rowA