get values of a pandas dataframe column as a list

Pandas – Get Column Values as a List

In this tutorial, we will know how to get the column values in a Pandas Dataframe as a list.
Later, we will understand the same with the help of a few examples.

We can get the values in a pandas dataframe column as a list in the following ways:

  • Using the tolist() function
  • Using the list() constructor

1. Using the tolist() function :

By using the pandas series tolist() function, we can create a list from the values of a pandas dataframe column.
We can directly apply the tolist() function to the column as shown in the syntax below.

Syntax: dataFrameName['ColumnName'].tolist()

2. Using list() constructor:

In order to get the column values as a list we can pass the column values as an argument to the list() constructor to create a list of the column values of the specified dataframe.

Syntax: list(dataFrameName["Column Name"])

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Examples

We will now look at a few examples for a better understanding.

But before that, we will create a pandas dataframe that we will be using throughout this tutorial using the following command:

import pandas as pd

# employee data
data = {
    "Name": ["Jim", "Dwight", "Angela", "Tobi"],
    "Age": [26, 28, 27, 32],
    "Department": ["Sales", "Sales", "Accounting", "HR"]
}

# create pandas dataframe
df = pd.DataFrame(data)
# displays dataframe
df

Output:

pandas dataframe with employee information

Example 1: Using the tolist() function

get values of a pandas dataframe column as a list
# get the values of the "Name" column as a list
df['Name'].tolist()

Output:

['Jim', 'Dwight', 'Angela', 'Tobi']

Example 2: Using the list() constructor

# get the values of the "Name" column as a list
list(df['Name'])

Output:

['Jim', 'Dwight', 'Angela', 'Tobi']

Summary

In this tutorial, we looked at how to get column values in a dataframe as a list. The following are the methods covered –

  • Use the pandas series tolist() method to get the column values as a list.
  • Alternatively, you can select the column and pass it to the list() constructor to get its values as a list.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

Scroll to Top