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.
In this tutorial, you’ll learn how to get a list from pandas dataframe columns.
If You’re in Hurry…
You can use the toList()
method to get the values of columns as a list.
values_list = df[“Column Name“].tolist()
values_list
If You Want to Understand Details, Read on…
Data is stored as rows and columns in the pandas dataframe. You may need to get all values in a column as a Python list to manipulate the data further.
There are different methods available that are appropriate in different scenarios. Let us look in detail at these different methods.
Table of Contents
Sample Dataframe
To demonstrate the different methods to get a list of column values in Pandas dataframe, first 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 Name | Last Name | Country | Country Code | |
---|---|---|---|---|
0 | Shivam | Pandey | India | 1 |
1 | Kumar | Ram | US | 2 |
2 | Felix | John | Germany | 3 |
3 | Michael | John | India | 4 |
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.
When you use the df[column_name], it returns a pandas series of a specific column. Then you can invoke the tolist()
method to convert the series to a Python list.
The below example demonstrates how to get the Country column as a list.
Code
Countries = df["Country"].tolist()
Countries
You’ll see the below output, and it contains all the values of the column(Including duplicate values).
Output
['India', 'US', 'Germany', 'India']
This is how you can use the tolist()
method available in the Pandas series.
Using Numpy toList()
In this section, you’ll learn how to use the tolist() method available in the Numpy array.
You can use this method when you already have the pandas dataframe column values as a NumPy array.
You can convert the pandas dataframe column to a NumPy array using the values attribute.
Once you have the Numpy array, you can use the tolist()
method to convert the array to a list.
The below example demonstrates how to convert the Country column to a Numpy array using values
and then invoke the tolist()
method to convert it into a list.
Code
Countries = df["Country"].values.tolist()
Countries
You’ll see the below output. All the values in the Country column are displayed, including the duplicate values.
Output
['India', 'US', 'Germany', 'India']
This is how you can use the tolist()
method available in the NumPy array to get a list of values from the Pandas dataframe.
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.
You can 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.
The below example demonstrates how to get the Country column as a list using the list() function.
Code
Countries = list(df["Country"])
Countries
You’ll see the below output. The output consists of all the values, including the duplicate values.
Output
['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.
This method returns the Array, not a list. The array can be converted to a list using the list()
function.
The example below demonstrates how to convert the Pandas series to a NumPy array using the to_numpy()
function and convert the array to a list using the list()
function.
Code
Countries = df["Country"].to_numpy()
list(Countries)
You’ll see the below output. The values are converted to a list, including duplicate values.
Output
['India', 'US', 'Germany', 'India']
Get Unique values as List from Pandas Dataframe Columns
In this section, you’ll learn how to get Unique values as a list from pandas Dataframe columns.
Using Pandas Series.Unique()
You can use the unique() method to get the unique values from the Dataframe column.
It’ll return a NumPy array with unique values. You can pass this array to the list()
function to get the list of unique values.
Code
unique_array = df["Country"].unique()
list(unique_array)
You’ll see the below output.
Output
['India', 'US', 'Germany']
Using Numpy Unique()
In this section, you’ll get a unique list of values from the Dataframe column using the NumPy unique() function.
It finds the unique elements in the NumPy array.
The below example demonstrates how to convert the Pandas Dataframe column to a NumPy array and get the unique values using the unique()
function.
import numpy as np
array = df["Country"].to_numpy()
unique_array = np.unique(array)
list(unique_array)
You’ll see the below output that contains only the unique values.
Output
['Germany', 'India', 'US']
Conclusion
You’ve learned how to get a list from pandas dataframe columns or rows.
Also, you’ve learned how to get the unique values from the columns as a list.