How to Convert Pandas DataFrame to List?

  • Post author:
  • Post category:Pandas
  • Post last modified:January 9, 2024
  • Reading time:13 mins read

Pandas tolist() function is used to convert Pandas DataFrame to a 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 the 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 Converting DataFrame to List

If you are in a 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("Create DataFrame:\n", df)

Yields below output.

Pandas DataFrame to list

2. Pandas Convert DataFrame to List using tolist()

To convert Pandas DataFrame to a list you can 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("After converting a DataFrame to a list:\n", list)

Yields below output.

Pandas DataFrame to list

Again, the df.values return values are 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 uses values.tolist() to convert the column values to a list.

You can consider that the columns of a DataFrame are Series objects hence, we can convert the columns of a DataFrame into a list using the tolist() method. Let’s use tolist() to convert one of the columns (series) of DataFrame to a list. For example,


# Convert DataFrame column as a list
list = df['Fee'].tolist()
print("After converting a DataFrame column to a list:\n", list)

Yields below output.


# Output:
# After converting a DataFrame column to a list:
 [22000, 25000, 23000, 24000, 26000]

4. Convert DataFrame to Nested List

You 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, you can df.columns to 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("After converting a DataFrame to a nested list:\n", list)

Yields below output.


# Output:
# After converting a DataFrame to a nested list:
[['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("After converting a index column to a list:\n", list)

Yields below output.


# Output:
# After converting a index column to a list:
 ['Courses', 'Fee', 'Duration', 'Discount']

Frequently Asked Questions on Convert Pandas DataFrame to List

How do I convert a Pandas DataFrame to a list?

You can use the .values attribute of the DataFrame to convert it to a NumPy array and then use the tolist() method. Forexample, list = df.values.tolist()

How can I convert a specific column of a DataFrame to a list?

You can apply the tolist() method directly on the column of DataFrame, it will convert those column values into list. For example, list = df[‘Column1’].tolist()

What if I want to convert multiple columns to a list?

You can use the .values attribute to get a NumPy array and then use the tolist() method. For example, list = df[['Column1', 'Column2']].values.tolist()

Are there any other methods to convert a DataFrame to a list?

You can use the to_numpy() method to convert the DataFrame to a NumPy array and then use the tolist() method. For example, list = df.to_numpy().tolist()

How can I convert the entire DataFrame or just a specific range of rows and columns to a list?

You can use the iloc[] or loc[] attributes to select a specific range of rows and columns before converting to a list. For example, list = df.iloc[1:3, 0:2].values.tolist()

6. Conclusion

In this article, you have learned how to convert Pandas DataFrame to a list by using Series.values.tolist() and also learned how to convert the DataFrame column and index column to a list with examples.

Happy Learning !!

References

Naveen (NNK)

Naveen (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ @ LinkedIn

Leave a Reply