How to Convert Pandas DataFrame to List?

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:February 4, 2023

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 CoursesFeeDurationDiscount.


# 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.

Pandas DataFrame to list
Convert DataFrame column to list

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.

convert DataFrame to list
Convert index column to list

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 !!

References

Leave a Reply

You are currently viewing How to Convert Pandas DataFrame to List?