Open In App

Get a list of a specified column of a Pandas DataFrame

Improve
Improve
Like Article
Like
Save
Share
Report

In this discussion, we’ll delve into the procedures involved in Get a list of a specified column of a Pandas DataFrame. Our journey commences with the essential task of importing data from a CSV file into a Pandas DataFrame. This initial step establishes the groundwork necessary to create an environment conducive to subsequent data manipulations.

Get a List of a specified column of a Pandas DataFrame

There are Various methods exist to Get a list of a specified column of a Pandas DataFrame. In this discussion, we will explore commonly employed techniques that are typically used to Get a list of a specified column of a Pandas DataFrame

  • Using series.tolist()
  • Using numpy.ndarray.tolist()
  • Python list() Function 
  • get()‘ Function in Python
  • .iloc[] Function in Python
  • List from Index column of Pandas Dataframe

Note: To get the CSV file used click here.

Create a simple DataFrame

The below code uses Pandas to read a CSV file (“nba.csv”) and creates a DataFrame. It then extracts the first five rows using the `head(5)` method, storing them in the variable `df`, and displays the resulting data.

Python3




# importing pandas module
import pandas as pd
   
# making data frame from csv
data = pd.read_csv("nba.csv")
   
# calling head() method 
df = data.head(5)
   
# displaying data
df


Output

Get a list of a specified column of a Pandas

Output

Let’s see how to get a list of a specified column of a Pandas DataFrame. We will convert the column “Name” into a list. 

Get List from Pandas Dataframe using series.tolist()

From the dataframe, we select the column “Name” using a [] operator that returns a Series object. Next, we will use the function Series.to_list() provided by the Series class to convert the series object and return a list.

Python3




# importing pandas module
import pandas as pd
 
# making data frame from csv
data = pd.read_csv("nba.csv")
df = data.head(5)
 
# Converting a specific Dataframe
# column to list using Series.tolist()
Name_list = df["Name"].tolist()
 
print("Converting name to list:")
 
# displaying list
Name_list


Output:

Get a list of a specified column of a Pandas

Output

Let’s break it down and look at the types 

Python3




# 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' >

Get List from Pandas Dataframe using numpy.ndarray.tolist()

With the help of numpy.ndarray.tolist(), dataframe we select the column “Name” using a [] operator that returns a Series object and uses Series.Values to get a NumPy array from the series object. Next, we will use the function tolist() provided by NumPy array to convert it to a list.

Python3




# Converting a specific Dataframe column
# to list using numpy.ndarray.tolist()
Name_list = df["Name"].values.tolist()
 
print("Converting name to list:")
 
# displaying list
Name_list


Output:

Get a list of a specified column of a Pandas

Output

Similarly, breaking it down 

Python3




# Select a column from dataframe
# as series and get a numpy array
print(type(df["Name"].values))
 
# Convert numpy array to a list
print(type(df["Name"].values.tolist()


Output:

<class 'numpy.ndarray' >
<class 'list' >

Get List from Pandas Dataframe using Python list() Function 

You can also use the Python list() function with an optional iterable parameter to convert a column into a list.

Python3




Name_List = list(df["Name"])
 
print("Converting name to list:")
 
# displaying list
Name_List


Output:

Get a list of a specified column of a Pandas

Output

Get List from Pandas Dataframe using Get() Function in Python

In this method below code uses Pandas to create a sample DataFrame with individual details. It extracts the ‘Salary’ column using the ‘get()’ method, converting it into a Python list with ‘tolist()’, and prints both the original DataFrame and the ‘Salary’ values list. The ‘get()’ method proves useful for column retrieval in Pandas

Python3




# 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
salary_list = df.get('Salary').tolist()
 
# Displaying the original DataFrame and the obtained list
print("Original DataFrame:")
print(df)
print("\nList of 'Salary' column:")
print(salary_list)


Output :

Original DataFrame:
Name Age Salary
0 Alice 25 50000
1 Bob 30 60000
2 Charlie 22 45000
List of 'Salary' column:
[50000, 60000, 45000]


Get List from Pandas Dataframe using iloc() Function in Python

In this example the .iloc[] function in Pandas is employed for integer-location based indexing. In the code snippet, df.iloc[:, 0] selects all rows of the first column (‘Name’), converting it to a Python list using tolist(). This approach provides a succinct means of extracting and handling data from the specified column based on integer positions in the DataFrame.

Python3




import pandas as pd
 
# Sample DataFrame
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
name_column = df.iloc[:, 0].tolist()
 
# Displaying the result
print("List of the 'Name' column:")
print(name_column)


Output :

List of the 'Name' column: 
['Geek1', 'Geek2', 'Geek3']


Get List from Index column of Pandas Dataframe

Index column can be converted to list, by calling pandas.DataFrame.index which returns the index column as an array and then calling index_column.tolist() which converts index_column into a list. 

Python3




# Converting index column to list
index_list = df.index.tolist()
 
print("Converting index to list:")
 
# display index as list
index_list


Output :

Converting index to list :
[0, 1, 2, 3, 4]


Last Updated : 01 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads