How to Get a List from Pandas Dataframe Columns – Definitive Guide

When working with Pandas, you may need to get a list of values in a column.

You can get a list from pandas dataframe columns using the df[“Column name“].tolist() statement.

Basic Example

values_list = df[“Column Name“].tolist()

values_list

This tutorial teaches you how to convert pandas dataframe columns to a list.

Sample Dataframe

Create a sample dataframe with Four columns.

Column names: First Name, Last Name, Country, Country Code

In the sample dataframe, the First Name column contains only distinct values and the Last Name, Country, and Country Code have duplicate values.

Code

import pandas as pd 

# List of Tuples
users = [ ('Shivam', 'Pandey', 'India', 1),
             ('Kumar', 'Ram' , 'US', 2 ),
         ('Felix','John' , 'Germany', 3 ),
         ('Michael','John' , 'India', 1 ),
              ]

#Create a DataFrame object
df = pd.DataFrame(  users, 
                    columns = ['First Name' , 'Last Name', 'Country', 'Country Code']
                    ) 

df

Dataframe Will Look Like

First NameLast NameCountryCountry Code
0ShivamPandeyIndia1
1KumarRamUS2
2FelixJohnGermany3
3MichaelJohnIndia4

Now let us see the different methods to get values as a list.

Using Series toList()

In this section, you’ll learn how to use the toList() method available in the Pandas Series.

  • Get the pandas series of a specific column using df[column_name]
  • Invoke the tolist() method to convert the series to a Python list.

Code

The following code demonstrates how to get the Country column as a list.

Countries = df["Country"].tolist()

Countries

Output

You’ll get the country column values as a list(Including duplicate values).

    ['India', 'US', 'Germany', 'India']

Using Numpy toList()

In this section, you’ll learn how to use the tolist() method available in the NumPy array.

Use this method when you already have the pandas dataframe column values as a NumPy array.

Code

Countries = df["Country"].values.tolist()

Countries

Output

All the values in the Country column are displayed, including the duplicate values.

    ['India', 'US', 'Germany', 'India']

Using Python List()

In this section, you’ll learn how to use the Python list() function to get a list of values from a column in the pandas dataframe.

  • Pass the Pandas series values using the df[Column name] to the list() function
  • It’ll return the Python list object with the list of values.

Code

Countries = list(df["Country"])

Countries

Output

The output consists of all the values, including the duplicate values.

    ['India', 'US', 'Germany', 'India']

Using to_numpy()

In this section, you’ll learn how to get a list of values from a Pandas Dataframe column using the to_numpy() method.

  • to_numpy() method returns the array, not a list.
  • Use the list() function to convert the array to a list.

Code

Countries = df["Country"].to_numpy()

list(Countries)                   

Output

The values are converted to a list, including duplicate values.

['India', 'US', 'Germany', 'India']

Get Unique values as List Using Pandas Series.Unique()

In this section, you’ll learn how to get Unique values as a list from pandas Dataframe columns.

  • Use the unique() method to get the unique values from the Dataframe column. It’ll return a NumPy array with unique values.
  • Pass this array to the list() function to get the list of unique values.

Code

unique_array = df["Country"].unique()

list(unique_array)

Output

   ['India', 'US', 'Germany']

These are the different methods to convert pandas dataframe columns to a list.

Additional Resources

Leave a Comment