Get a List of a Specific Column of a Pandas DataFrame
Last Updated :
18 Mar, 2025
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.
Similar Reads
Split a column in Pandas dataframe and get part of it
When a part of any column in Dataframe is important and the need is to take it separate, we can split a column on the basis of the requirement. We can use Pandas .str accessor, it does fast vectorized string operations for Series and Dataframes and returns a string object. Pandas str accessor has nu
2 min read
Get a List of Particular Column Values in a Pandas DataFrame
In this article, you'll learn how to extract all values of a particular column from a Pandas DataFrame as a Python list. Get a List of a Particular Column Using tolist()tolist() method is a simple and effective way to convert a Pandas Series (column) into a Python list. Here's an example:Pythonimpor
2 min read
Insert a given column at a specific position in a Pandas DataFrame
In this comprehensive guide, we will leverage the powerful DataFrame.insert() the method provided by the Pandas library to effectively Insert a given column at a specific position in a Pandas Dataframe. Create a Sample DataFrame In this example below code uses Pandas to create a DataFrame named 'df'
4 min read
Get a specific row in a given Pandas DataFrame
In the Pandas Dataframe, we can find the specified row value with the function iloc(). In this function, we pass the row number as a parameter. The core idea behind this is simple: you access the rows by using their index or position. In this article, we'll explore different ways to get a row from a
5 min read
Percentile rank of a column in a Pandas DataFrame
Let us see how to find the percentile rank of a column in a Pandas DataFrame. We will use the rank() function with the argument pct = True to find the percentile rank. Example 1 : Python3 # import the module import pandas as pd # create a DataFrame data = {'Name': ['Mukul', 'Rohan', 'Mayank', 'Shubh
1 min read
Get list of column headers from a Pandas DataFrame
In this article, we will see, how to get all the column headers of a Pandas DataFrame as a list in Python. The DataFrame.column.values attribute will return an array of column headers. pandas DataFrame column namesUsing list() Get Column Names as List in Pandas DataFrame In this method we are using
3 min read
Get the datatypes of columns of a Pandas DataFrame
Let us see how to get the datatypes of columns in a Pandas DataFrame. TO get the datatypes, we will be using the dtype() and the type() function.Example 1 :Â Â python # importing the module import pandas as pd # creating a DataFrame dictionary = {'Names':['Simon', 'Josh', 'Amen', 'Habby', 'Jonathan',
2 min read
Get last n records of a Pandas DataFrame
Let's discuss how to get last n records of a Pandas DAtaframe. There can be various methods to get the last n records of a Pandas DataFrame. Lets first make a dataframe:Example: Python3 # Import Required Libraries import pandas as pd import numpy as np # Create a dictionary for the dataframe dict =
2 min read
Highlight Pandas DataFrame's specific columns using apply()
Let us see how to highlight specific columns of a Pandas DataFrame. We can do this using the apply() function of the Styler class. Styler.apply() Syntax : Styler.apply(func, axis = 0, subset = None, **kwargs) Parameters : func : function should take a Series or DataFrame (depending on-axis), and ret
2 min read
How to Delete a column from Pandas DataFrame
Deleting data is one of the primary operations when it comes to data analysis. Very often we see that a particular column in the DataFrame is not at all useful for us and having it may lead to problems so we have to delete that column. For example, if we want to analyze the students' BMI of a partic
2 min read