Convert pandas DataFrame Column to List in Python (Example Code)

In this article you’ll learn how to convert the variable of a pandas DataFrame to a list in Python programming.

Preparing the Example

import pandas as pd                                         # Import pandas library
my_df = pd.DataFrame({'A':['a', 'b', 'x', 'y', 'a', 'b'],  # Construct DataFrame in Python
                      'B':range(1, 7),
                      'C':['hello', 'hey', 'jaja', 'juhu', 'hi', 'yoyo'],
                      'D':[1, 0, 5, 2, 1, 7]})
print(my_df)                                               # Display DataFrame in console
#    A  B      C  D
# 0  a  1  hello  1
# 1  b  2    hey  0
# 2  x  3   jaja  5
# 3  y  4   juhu  2
# 4  a  5     hi  1
# 5  b  6   yoyo  7

Example: Using tolist() Function to Get pandas DataFrame Column as List

my_df['C'].tolist()
# ['hello', 'hey', 'jaja', 'juhu', 'hi', 'yoyo']

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top