• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:13 mins read
You are currently viewing How to Convert Pandas Column to List

You can get or convert the pandas DataFrame column to the list using Series.values.tolist(), since each column in DataFrame is represented as a Series internally, you can use this function after getting a column you wanted to convert as a Series. You can get a column as a Series by using df.column_name or df['column_name'].

In this article, I will explain how to convert a column from a DataFrame to a list using the .tolist() method on the specific column.

1. Quick Examples of Convert Column to List

Following are quick examples of converting column values to a list in DataFrame.


# Quick examples of converting Pandas column to list 

# Example 1: Using Series.values.tolist()
col_list = df.Courses.values.tolist()

# Example 2: Using Series.values.tolist()
col_list = df["Courses"].values.tolist()

# Example 3: Using list() Function
col_list =  list(df["Courses"])

# Example 4: Convert Pandas column to numpy array
col_list = df['Courses'].to_numpy()

# Example 5: Get the list by Column Index
col_list = df[df.columns[0]].values.tolist()

# Example 6: Convert Index Column to List
index_list = df.index.tolist()

Let’s create a Pandas DataFrame with a Python dictionary of lists, pandas DataFrame columns names CoursesFeeDurationDiscount.


# Create DataFrame
import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","Pandas","Python","Spark","Pandas"],
    'Fee' :[20000,25000,22000,30000,25000,20000,30000],
    'Duration':['30days','40days','35days','50days','40days','30days','50days'],
    'Discount':[1000,2300,1200,2000,2300,1000,2000]
              }
df = pd.DataFrame(technologies)
print("Create DataFrame:\n", df)

Yields below output.

Pandas convert column list

2. Convert the Pandas Column to a List

You can use Series.values.tolist() to convert Pandas DataFrame Column to a list. df[‘Courses’] returns the DataFrame column as a Series and then uses this function to convert the column values to a list.

Let’s apply tolist() method to the specified column of DataFrame and get a Python list containing the values from that column.


# Using Series.values.tolist() & get the list
col_list = df.Courses.values.tolist()
print("Get the list from DataFrame column:\n", col_list)
Pandas convert column list

Below is an explanation of each section of the statement df.Courses.values.tolist().

  • df['Courses'] returns a Series object of a specified column.
  • df['Courses'].values returns an array with column values and this has a helper function .tolist() to convert to a list.

Alternatively, you can also write the statement using.


# Using Series.values.tolist()
col_list = df["Courses"].values.tolist()

Related: To get unique values in column as a list use the unique() function on the Series object.

3. Using list() Convert Pandas Column to list

You can also use the Python list() function to convert the Pandas column to a list. Simply, pass the specified column of DataFrame which we want to convert as a list into this function. It will return the list with the values of the specified column of DataFrame.


# Using list() Function to get the list
col_list =  list(df["Discount"])
print("Get the list from specified column:\n", col_list)

# Output:
# Get the list from specified column:
# [1000, 2300, 1200, 2000, 2300, 1000, 2000]

4. Convert Columns to Numpy Array

Sometimes you may be required to convert the Pandas column to Numpy Array you can do so by using the to_numpy() function.


# Conver to numpy.to_numpy() array
col_list = df['Courses'].to_numpy()
print("Get the array from specified column:\n", col_list)
print(type(col_list))

# Output:
# Get the array from specified column
# ['Spark' 'PySpark' 'Python' 'Pandas' 'Python'
# 'Spark' 'Pandas']
# <class 'numpy.ndarray'>

5. Get List by Column Index

If you have a column index and want to get the column values of an index, first you have to get the column name by index and then use the approaches explained above to convert it to a list. From the below example df.columns[0] returns the column name for an index 0, which is Courses.


# Get by Column Index
col_list = df[df.columns[0]].values.tolist()
print("Get the list from specified column:\n", col_list)

# OUtput: 
# Get the list from specified column:
# ['Spark', 'PySpark', 'Python', 'Pandas', 'Python', 'Spark', 'Pandas']

6. Convert the Index Column to a List

In case you want to get the index column as a list use DataFrame.index.tolist() method. Even here DataFrame.index returns the Index object and tolist() converts it to list.


# Convert Index Column to List
index_list = df.index.tolist()
print(index_list)

# Output:
# [0, 1, 2, 3, 4, 5, 6]

7. Complete Example of pandas Conver Column to List


import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark","PySpark","Python","Pandas","Python","Spark","Pandas"],
    'Fee' :[20000,25000,22000,30000,25000,20000,30000],
    'Duration':['30days','40days','35days','50days','40days','30days','50days'],
    'Discount':[1000,2300,1200,2000,2300,1000,2000]
              }
df = pd.DataFrame(technologies)

# Using Series.values.tolist()
col_list = df.Courses.values.tolist()

# Using Series.values.tolist()
col_list = df["Courses"].values.tolist()

# Using list() Function
col_list =  list(df["Courses"])

# Get by Column Index
col_list = df[df.columns[0]].values.tolist()

# Convert Index Column to List
index_list = df.index.tolist()

Frequently Asked Questions of pandas Conver Column to List

How do I convert a specific column to a list in pandas?

You can convert a specific column in a pandas DataFrame to a list by using the .tolist() method on the column.

How do I convert a column to a list and then perform operations on it?

Once you’ve converted a column to a list, you can perform various operations on it like any other Python list.

Is it possible to convert a column to a list with a specific data type?

By default, the .tolist() method will convert the column to a list with the same data type as the column’s data. If you want to enforce a specific data type, you can use list comprehensions to cast the data to the desired type

Can I convert an entire DataFrame to a list?

Yes, you can convert an entire DataFrame to a list of lists using the .values.tolist() method. Each row in the DataFrame will be converted to a list, and the list of lists represents the entire DataFrame.

Conclusion

In this article, you have learned how to get the column values as a list by using several methods like Series.values, list(), and also learned to convert the index column to a list with examples.

Happy Learning !!

References

Naveen Nelamali

Naveen Nelamali (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 and Medium