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.
Key Points –
- Use the
.values.tolist()
method to convert the entire DataFrame into a list of lists. - Use the
.tolist()
method on a specific column to convert it into a one-dimensional list. - Select multiple columns and use
.values.tolist()
to convert them into a list of lists. - Use
.tolist()
on a single column to convert it to a flat list. - Use the
.index.tolist()
method to convert the DataFrame index into a list. - Use
.to_numpy().tolist()
as an alternative to.values.tolist()
for converting the DataFrame to a list.
Quick Examples of Converting DataFrame to List
Below are some quick examples of converting DataFrame to a list.
# Quick examples of converting dataframe to list
# 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()
To run some examples of converting Pandas DataFrame to a list, let’s create Pandas DataFrame using data from a dictionary.
# 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.
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.
Again, the df.values
return values are present in the DataFrame. tolist()
will convert those values into a list.
Convert Pandas Column to List
To convert a specific column of a Pandas DataFrame into a list, you can directly access that column by its name and convert it using the tolist()
method. 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.
# 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]
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]]
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']
FAQ on Converting DataFrame to 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()
You can convert multiple columns of a DataFrame to lists simultaneously by accessing those columns as a subset of the DataFrame and then calling the tolist()
method.
When converting a DataFrame to a list, keep in mind that each row in the DataFrame will be converted to a sublist in the list, where the elements of the sublist correspond to the values in each column of the respective row.
You can also convert a DataFrame to a list using list comprehensions or other methods in Python, but using the tolist()
method is often the simplest and most efficient way.
Conclusion
In summary, converting Pandas DataFrame to a list using Series.values.tolist()
. Additionally, you have learned how to convert DataFrame columns and index columns into lists.
Happy Learning !!
Related Articles
- convert Pandas index to list?
- Pandas Convert String to Integer
- Convert Pandas Series to DataFrame
- Convert Pandas DataFrame to Series
- Convert Pandas Series to NumPy Array
- Convert Pandas Datetime Index to String
- Pandas Convert Dictionary to DataFrame
- Convert Pandas DataFrame to NumPy Array
- Pandas Convert Integer to String in DataFrame
- Pandas Convert Floats to Strings in DataFrame
- Pandas Convert Boolean to String in DataFrame
- Convert Multiple Columns to String in Pandas DataFrame