Pandas tolist()
function is used to convert Pandas DataFrame to list. In Python, pandas is the most efficient library for providing various functions to convert one data structure to another data structure. DataFrame is a two-dimensional data structure and it consists of rows and columns in form of a tabular format, which is used to store the data. Whereas a list is a single-dimensional data structure and it stores the collection of Data.
Using df.values().tolist()
syntax we can easily convert Pandas DataFrame to a list. In this article, I will explain the tolist() function and using this how we can convert pandas DataFrame to a Python list, and also I explain how we can convert the Pandas DataFrame column to a list with several examples.
1. Quick Examples of Convert DataFrame to List
If you are in hurry, below are some quick examples of how to convert DataFrame to a list.
# Below are some quick examples.
# Example 1: Convert DataFrame to list using tolist()
list = df.values
print(list.tolist())
# Example 2: Convert DataFrame column as a list
print(df['Fee'].tolist())
# Example 3: Create DataFrame to nested list
# Create an empty list
list = []
# Iterate through the columns of dataframe
for column in df.columns
list1 = df[column].tolist()
list.append(list1)
print(list)
# Example 4: Convert index column to list
list = df.columns.values.tolist()
Let’s create a Pandas DataFrame with a Python dictionary of lists, pandas DataFrame columns names Courses
, Fee
, Duration
, Discount
.
# Create DataFrame
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
'Fee' :[22000,25000,23000,24000,26000],
'Duration':['30days','50days','35days', '40days','35days'],
'Discount':[1000,2300,1000,1200,2500]
}
df = pd.DataFrame(technologies)
print(df)
To create a DataFrame, we need Data. So, we have created a Dictionary in which keys are column names and values are the lists of values.
2. Pandas Convert DataFrame to List using tolist()
In order to convert Pandas DataFrame to list use df.values.tolist()
Here, df.values
returns a DataFrame as a NumPy array. And, tolist()
converts Numpy to list. Please remember that only the values in the DataFrame will be returned, and the axes labels will be removed.
# Convert DataFrame to list using df.values.tolist()
list = df.values.tolist()
print(list)
Yields below output.
# Output:
[['Spark', 22000, '30days', 1000], ['PySpark', 25000, '50days', 2300], ['Hadoop', 23000, '35days', 1000], ['Python', 24000, '40days', 1200], ['Pandas', 26000, '35days', 2500]]
Again, the df.values
return values present in the dataframe. tolist()
will convert those values into a list.
3. Convert Pandas Column to List
By using Series.values.tolist()
you can convert the Pandas DataFrame Column to List. df[‘Courses’]
returns the DataFrame column as a Series and then use values.tolist()
to convert the column values to list.
We consider that the columns of a DataFrame are Series objects hence, we can convert the columns of DataFrame into a list using the tolist() method. Let’s use tolist() to convert one of the columns (series) of DataFrame to list. For example,
# Convert DataFrame column as a list
print(df['Fee'].tolist())
Yields below output.

4. Convert DataFrame to Nested List
We can convert DataFrame to the nested list by iterating all columns of the given DataFrame and these values are assigned into the temporary list. Finally, using append() we can append the temporary list to an empty list, where the lists are all column values of a given DataFrame. Here, df.columns get columns from DataFrame
# Create DataFrame to nested list
# Create an empty list
list = []
# Iterate through the columns of dataframe
for column in df.columns
list1 = df[column].tolist()
list.append(list1)
print(list)
Yields below output.
# Output:
[['Spark', 'PySpark', 'Hadoop', 'Python', 'Pandas'], [22000, 25000, 23000, 24000, 26000], ['30days', '50days', '35days', '40days', '35days'], [1000, 2300, 1000, 1200, 2500]]
5. Convert Pandas Index Column to List
We can convert the pandas DataFrame index column to List using the below syntax.
# Convert index column to list
list = df.columns.values.tolist()
print(list)
Yields below output.

6. Conclusion
In this article, you have learned how to convert Pandas DataFrame to list by using several methods like Series.values.tolist() and also learned to convert the index column to list with examples.
Happy Learning !!
Related Articles
- How to convert Pandas index to list?
- Convert Pandas DataFrame to Series
- Convert Pandas Series to String
- Convert Pandas Series of Lists to One Series
- Convert Pandas Series to NumPy Array
- Convert Pandas Datetime Index to String
- Convert Pandas Series to DataFrame
- How to Convert pandas Column to List
- Convert Pandas DataFrame to NumPy Array
- Pandas Convert String to Integer