Python developers commonly use Pandas dataframe to store data in tabular manner and analyze them using rows and columns. Sometimes, they may need to convert a Pandas dataframe to list in Python. This may be because they need to export data in a specific format or some other reason. In this article, we will learn how to convert Pandas dataframe to list.
How to Convert Pandas Dataframe to List
Here are the different ways to transform Pandas dataframe to list. Let us say you have the following Pandas dataframe.
import pandas as pd
data = {'name':['john','jane','joe','jim'],
'age': [12,15,14,16] }
df = pd.DataFrame(data)
print(df)
## output
name age
0 john 12
1 jane 15
2 joe 14
3 jim 16
Every Pandas dataframe readily supports tolist() function that allows you to convert a dataframe into list.
1. Convert one dataframe column to list
In this solution, we will learn how to convert one dataframe column into Python list. For this purpose, we will call tolist() function on the result of data for a single column. You can get the result of a single column using dataframe[column_name] command. Here is the command to get the result of name column.
print(df['name'])
## output
0 john
1 jane
2 joe
3 jim
When you call tolist() function on this column data, it will return the result as a list.
print(df['name'].tolist())
## output
['john', 'jane', 'joe', 'jim']
Similarly, you can convert every column’s data into a list.
2. Convert all columns into lists
In this approach, we will convert the given dataframe into a nested list of lists. For this, we loop through all the columns of dataframe, and for each column, we call the tolist() function, as we did in earlier approach. You can get a list of all column names using df.columns property.
print(df.columns) # output is Index(['name', 'age'], dtype='object')
We will loop through the output of df.columns to print the result of each column.
for column in df.columns:
print(df[column].tolist())
## output
['john', 'jane', 'joe', 'jim']
[12, 15, 14, 16]
3. Convert Dataframe into Nested List
In the previous solution, we converted each column of dataframe into a separate list. In this approach, we will learn how to convert dataframe into nested list of lists, where each list contains data pertaining to one column. For this purpose, we use dataframe.values property that returns series object.
print(df.values)
## output
[['john' 12]
['jane' 15]
['joe' 14]
['jim' 16]]
We directly call tolist() function on the above output.
print(df.values.tolist())
## output
[['john', 12], ['jane', 15], ['joe', 14], ['jim', 16]]
4. Convert Dataframe into list with column names
In all the above solutions, we have converted dataframe data into list, but excluded the column names. In this approach, we will also include columns names as another list. To get column names of a dataframe, we call dataframe.columns.values. We convert it into list using tolist() function.
print(df.columns.values.tolist()) # output is ['name', 'age']
We concatenate the result of above function to nested list obtained in previous solution.
print([df.columns.values.tolist()]+ df.values.tolist())
## output
[['name', 'age'], ['john', 12], ['jane', 15], ['joe', 14], ['jim', 16]]
In the above code, we have stored the result of dataframe.columns.values.tolist() in an empty list. To this we have concatenated the result of dataframe.values.tolist() using concatenation operator ‘+’.
Conclusion
In this article, we have learnt several different ways to convert pandas dataframe to list. You can use any of these methods as per your requirement. But it is important to note that all these solutions use tolist() function, called at different places. In some cases, we call it for the output of dataframe[column_name], whereas in some cases we call it on output of dataframe.values. Depending on where you call tolist() function, its result differs and accordingly, you need to proceed.
Also read:
How to Compare Two Lists in Python
How to Create Dictionary from Lists
How to Remove Duplicates from List

Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.