Open In App

Get a List of a Specific Column of a Pandas DataFrame

Last Updated : 18 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In data analysis, extracting specific columns from a DataFrame and converting them into Python lists is a common requirement. Pandas provides multiple ways to achieve this efficiently. This article explores various methods to extract a specific column from a Pandas DataFrame and convert it into a list.

Using tolist()

One of the most direct methods to convert a DataFrame column to a list is using thetolist() method from the Series object.

Dataset Link: nba.csv

Python
import pandas as pd

df = pd.read_csv("nba.csv")

# Displaying the first five rows
display(df.head(5))

# Convert the 'Name' column to a list
res = df['Name'].tolist()
print(res)

Output:

['Avery Bradley', 'Jae Crowder', 'John Holland', 'R.J. Hunter', 'Jonas Jerebko', 
'Amir Johnson', ... 'Shelvin Mack', 'Raul Neto', 'Tibor Pleiss', 'Jeff Withey', nan]

Explanation: df['Name'] extracts the column as a Pandas Series and .tolist() method converts this Series into a Python list.

Let’s break it down and look at their types for better understanding:

Python
# column 'Name' as series object
print(type(df["Name"]))

# Convert series object to a list
print(type(df["Name"].values.tolist()))

Output

<class 'pandas.core.series.Series' >
<class 'list' >

Besides tolist() method, there are other ways to retrieve a list from a specific column of a Pandas DataFrame, lets look at some of them:

Using numpy.ndarray.tolist()

You can also use numpy.ndarray.tolist() by first accessing the column as a Series and then converting it to a NumPy array with .values.

Python
import numpy as np

# Convert the 'Name' column to a NumPy array and then to a list
res = df['Name'].values.tolist()
print(res)

Output

['Avery Bradley', 'Jae Crowder', 'John Holland',  ... 'Shelvin Mack', 'Raul Neto', 'Tibor Pleiss', 'Jeff Withey', nan]

Explanation: df['Name'].values converts the column into a NumPy array and .tolist() is called on the NumPy array to convert it into a Python list.

Using Python's list() Function 

You can use Python's built-inlist() function to convert the column directly into a list.

Python
# Convert the 'Team' column to a list
res = list(df['Team'])
print(res)

Output

['Boston Celtics', 'Boston Celtics', 'Boston Celtics', ... , 'Utah Jazz', 'Utah Jazz', 'Utah Jazz', nan]

Explanation: list(df['Team']) converts the Series into a list without requiring additional methods.

Using get() Function in Python

get() function in Pandas can be used to access a specific column. After retrieving the column, you can convert it into a list using tolist().

Python
import pandas as pd

# Creating a sample DataFrame
data = {'Name': ['Geek1', 'Geek2', 'Geek3'],
        'Age': [25, 30, 22],
        'Salary': [50000, 60000, 45000]}

df = pd.DataFrame(data)

# Using 'get()' function to retrieve the 'Salary' column as a list
res = df.get('Salary').tolist()
print(res)

Output :

[50000, 60000, 45000]

Explanation: list(df['Team']) converts the Series into a list without requiring additional methods.

Using iloc[]

The.iloc[] method is used for integer-location based indexing. By selecting a column based on its integer position (e.g., the first column), you can convert it to a list.

Python
import pandas as pd

data = {'Name': ['Geek1', 'Geek2', 'Geek3'],
        'Team': ['Celtics', 'Celtics', 'Celtics'],
        'Number': [0, 99, 30],
        'Position': ['PG', 'SF', 'SG']}

df = pd.DataFrame(data)

# Using .iloc[] to get a list of the 'Name' column
res = df.iloc[:, 0].tolist()

print(res)

Output :

['Geek1', 'Geek2', 'Geek3']

Explanation:df.get('Salary') retrieves the column safely, avoiding errors if the column is missing and the .tolist() converts the Series into a list.


Next Article

Similar Reads