How To Convert Pandas Column To List
One of the common tasks when working with a DataFrame in Pandas is converting a column to a list. In this article we will learn how to convert a Pandas column to a list using various methods.
1. Using tolist()
One can convert a pandas column to a list using tolist() function which works on the Pandas Series object. This method is best for quickly converting a single column into a list for general data processing or iteration.
Converting "Name" Column to a List
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'],
'Age': [28, 34, 22, 45, 31],
'Salary': [60000, 75000, 50000, 90000, 65000],
'Gender': ['Female', 'Male', 'Male', 'Male', 'Female'],
}
df = pd.DataFrame(data)
# Using Series.values.tolist()
name_list = df['Name'].values.tolist()
print("Name list:", name_list)
Output:
Name list: ['Alice', 'Bob', 'Charlie', 'David', 'Eva']
2. Using Python's list()
Function
You can also use Python’s built-in list() function to convert a column into a list. This method is used when you prefer a simpler syntax or need the flexibility of Python's built-in list functionality.
Converting "Age" Column to a List
# Using list() Function
age_list = list(df['Age'])
print("Age list:", age_list)
Output:
Age list: [28, 34, 22, 45, 31]
This method directly converts the column into a list without using .values.
3. Using Column Index
If you prefer working with column indexes rather than names, you can use the column index to select the desired column and then convert it to a list. This method is ideal when you are working with DataFrames where column positions are fixed or dynamically accessed.
Converting "Gender" Column Using its Index
# Get List by Column Index
gender_list = df[df.columns[3]].values.tolist()
print("Gender list:", gender_list)
Output:
Gender list: ['Female', 'Male', 'Male', 'Male', 'Female']
Here df.columns[3] refers to the 4th column i.e. "Gender". You can adjust the index to select any other column.
4. Converting Index Column to a List
Sometimes you might want to convert the index of the DataFrame into a list. You can do this using the .index attribute:
Converting Index to a List
# Convert the Index Column to a List
index_list = df.index.tolist()
print("Index list:", index_list)
Output:
Index list: [0, 1, 2, 3, 4]
5. Using Numpy Array
If you need the column values in a NumPy array instead of a list you can use .to_numpy() to convert the column directly. This method is ideal for numerical computations where performance and array-specific functions are needed.
Converting "Salary" Column to a NumPy Array
# Convert Columns to Numpy Array
salary_array = df['Salary'].to_numpy()
print("Salary array:", salary_array)
Output:
Salary array: [60000 75000 50000 90000 65000]
This is especially useful when you need to perform numerical operations that are optimized for NumPy arrays.
6. Using iteritems()
Method
For more granular control over the iteration process you can use the iteritems()
method. This method allows you to iterate over each element in the column along with its index. You can then collect these elements into a list.
Converting "Age" Column
#using iteritem() function
age_list = [value for index, value in df['Age'].iteritems()]
print("Age:",age_list)
Output:
Age: [28, 34, 22, 45, 31]
7. Using List Comprehension
If you prefer using Python’s list comprehension technique you can achieve the same result. List comprehension provides a concise way to create lists and can be used to modify the data while extracting the column.
Converting "Names" Column
#using list comprehension
names_above_30 = [name for name, age in zip(df['Name'], df['Age']) if age > 30]
print("Names:",names_above_30)
Output:
Names: ['Bob', 'David', 'Eva']
Converting a Pandas column to a list is a common task in data analysis and can be done in several ways depending on the situation and need:
.values.tolist()
: The most common and efficient method for converting a pandas column to a list.list()
function: Simple and direct way to convert a column to a list without.values
.- By Index: Convert a column using its position with
df.columns[index]
. - Index to List: Convert the DataFrame index to a list using
.index
. - NumPy Array: Use
.to_numpy()
for numerical operations. iteritems()
: Iterate over column elements withiteritems()
to convert to a list.- List Comprehension: Efficient way to convert and transform a column to a list in one step.