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
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 which I have published on my YouTube channel. I show the examples of this tutorial and explain the Python syntax in some more detail:
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.
If you accept this notice, your choice will be saved and the page will refresh.
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.
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:
- Introduction to List in Python
- How to Use the pandas Library in Python
- Get Values of First Row in pandas DataFrame
- Get Column Names of pandas DataFrame as List
- Python Programming Examples
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.