Skip to content
Topics
Pandas
How to Convert Pandas DataFrame to List?

How to Convert Pandas DataFrame to List?

As a data scientist, you may come across situations where you need to convert a Pandas DataFrame to a list. Fortunately, this is quite easy to accomplish using the tolist() function. In this article, we'll explain how to convert a DataFrame to a list using this method. We'll also cover how to convert a column to a list, how to create a nested list, and provide some quick examples.

Want to quickly create Data Visualizations in Python?

PyGWalker is an Open Source Python Project that can help speed up the data analysis and visualization workflow directly within a Jupyter Notebook-based environments.

PyGWalker (opens in a new tab) turns your Pandas Dataframe (or Polars Dataframe) into a visual UI where you can drag and drop variables to create graphs with ease. Simply use the following code:

pip install pygwalker
import pygwalker as pyg
gwalker = pyg.walk(df)

You can run PyGWalker right now with these online notebooks:

And, don't forget to give us a ⭐️ on GitHub!

Run PyGWalker in Kaggle Notebook (opens in a new tab)Run PyGWalker in Google Colab (opens in a new tab)Give PyGWalker a ⭐️ on GitHub (opens in a new tab)
Run PyGWalker in Kaggle Notebook (opens in a new tab)Run PyGWalker in Google Colab (opens in a new tab)Run PyGWalker in Google Colab (opens in a new tab)

Converting a DataFrame to a List

The tolist() function is a built-in method in Pandas that converts a DataFrame to a list. You simply call this function on your DataFrame, and it will return a list.

import pandas as pd
 
# create a sample DataFrame
df = pd.DataFrame({'numbers': [1, 2, 3], 'letters': ['a', 'b', 'c']})
 
# convert DataFrame to list
list_from_df = df.values.tolist()
print(list_from_df)

This code will output:

[[1, 'a'], [2, 'b'], [3, 'c']]

As you can see, the tolist() function returns a list of lists, where each inner list corresponds to a row in the DataFrame. The values are ordered by column, and each value is of the type that Pandas infers from the values in the column.

Converting a Column to a List

There are also situations where you'll just want to convert a specific column from a DataFrame to a list. Here's how to do that:

import pandas as pd
 
# create a sample DataFrame
df = pd.DataFrame({'numbers': [1, 2, 3], 'letters': ['a', 'b', 'c']})
 
# convert 'numbers' column to list
list_from_column = df['numbers'].tolist()
print(list_from_column)

This code will output:

[1, 2, 3]

As you can see, the tolist() function is used on a specific column, and it returns a list with just the values from that column.

Creating a Nested List

In some situations, you may need to create a nested list from a DataFrame. For example, you may want a list of tuples for each row instead of a list of lists. Here's how to do that:

import pandas as pd
 
# create a sample DataFrame
df = pd.DataFrame({'numbers': [1, 2, 3], 'letters': ['a', 'b', 'c']})
 
# create a nested list
nested_list = [tuple(x) for x in df.to_records(index=False)]
print(nested_list)

This code will output:

[(1, 'a'), (2, 'b'), (3, 'c')]

As you can see, we used a list comprehension to create a list of tuples from the DataFrame. This can be useful if you need to pass your data to a function that expects a certain data structure.

Quick Examples

Here are a few quick examples of converting a DataFrame to a list:

import pandas as pd
 
# create a sample DataFrame
df = pd.DataFrame({'numbers': [1, 2, 3], 'letters': ['a', 'b', 'c']})
 
# convert DataFrame to list (method 1)
list_from_df1 = df.values.tolist()
 
# convert DataFrame to list (method 2)
list_from_df2 = [list(x) for x in df.values]
 
# convert 'numbers' column to list
list_from_column = df['numbers'].tolist()
 
# create a nested list
nested_list = [tuple(x) for x in df.to_records(index=False)]

In the first example, we used the tolist() function to convert the DataFrame to a list of lists.

In the second example, we used a list comprehension to achieve the same result.

In the third example, we used the tolist() function on a specific column to convert it to a list.

And in the fourth example, we used a list comprehension to create a nested list of tuples.

Conclusion

Converting a Pandas DataFrame to a list is a common task in data science. Thankfully, it's also quite simple using the tolist() function. Whether you need to convert the entire DataFrame to a list, a specific column to a list, or create a nested list, this function has got you covered. With the examples provided in this article, you'll be able to quickly and easily accomplish this task in your own projects.