Get pandas DataFrame Column as List in Python (2 Examples)

 

In this tutorial, I’ll demonstrate how to convert the column of a pandas DataFrame to a list in the Python programming language.

Table of contents:

It’s time to dive into the examples:

 

Example Data & Add-On Libraries

If we want to use the functions of the pandas library, we first have to load pandas:

import pandas as pd                             # Import pandas library

As a next step, we’ll also need to construct some data that we can use in the examples below:

data = pd.DataFrame({'x1':range(6, 0, - 1),    # Create example DataFrame
                     'x2':[1, 7, 5, 5, 3, 1],
                     'x3':['A', 'B', 'C', 'D', 'E', 'F']})
print(data)                                    # Print example DataFrame

 

table 1 DataFrame get pandas dataframe column as list python

 

As you can see based on Table 1, our example data is a DataFrame consisting of six rows and the three columns “x1”, “x2”, and “x3”.

 

Example 1: Convert Column of pandas DataFrame to List Using tolist() Function

This example shows how to get the values of a pandas DataFrame variable as a list object in Python.

For this, we can use the tolist function as shown below:

x3_list1 = data['x3'].tolist()                 # Get row indices
print(x3_list1)
# ['A', 'B', 'C', 'D', 'E', 'F']

The previous output shows that we have created a new list object called x3_list1 that contains the values of the column x3.

 

Example 2: Convert Column of pandas DataFrame to List Using to_numpy() Function

Alternatively to the tolist function (shown in Example 1), we can also use the to_numpy function to return the values of a DataFrame column as a list.

Consider the following Python syntax:

x3_list2 = data['x3'].to_numpy()               # Get row indices
print(x3_list2)
# ['A', 'B', 'C', 'D', 'E', 'F']

The output is exactly the same as in Example 1. However, this time we have used the to_numpy function instead of the tolist function.

 

Video, Further Resources & Summary

Have a look at the following video on the CS Dojo YouTube channel, in case you are looking for more info. In the video, the speaker demonstrates how to use lists in Python.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

In addition, you could have a look at the other articles on this homepage. You can find some articles below:

 

This tutorial has illustrated how to get the variable of a pandas DataFrame as list in Python programming. Please let me know in the comments section, in case you have further questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top