Convert Pandas DataFrame Column to List

  1. Use the tolist() Method to Convert a Dataframe Column to a List
  2. Use the list() Function to Convert a Dataframe Column to a List

This tutorial article will introduce different methods to convert a Pandas DataFrame column to a list, like using the tolist() method in Pandas.

Use the tolist() Method to Convert a Dataframe Column to a List

A column in the Pandas dataframe is a Pandas Series. So if we need to convert a column to a list, we can use the tolist() method in the Series. tolist() converts the Series of pandas data-frame to a list.

In the code below, df['DOB'] returns the Series, or the column, with the name as DOB from the DataFrame.

The tolist() method converts the Series to a list.

import pandas as pd

df=pd.DataFrame([
        ['James',   '1/1/2014',    '1000'],
        ['Michelina',   '2/1/2014',    '12000'],
        ['Marc',   '3/1/2014',    '36000'],
        ['Bob',   '4/1/2014',    '15000'],
        ['Halena',   '4/1/2014',    '12000']
        ], columns=['Name', 'DOB','Salary'])

print("Pandas DataFrame:\n\n",df,"\n")

list_of_single_column = df['DOB'].tolist()

print("the list of a single column from the dataframe\n",
        list_of_single_column,
        "\n",
        type(list_of_single_column))

Output:

Pandas DataFrame:

         Name       DOB Salary
0      James  1/1/2014   1000
1  Michelina  2/1/2014  12000
2       Marc  3/1/2014  36000
3        Bob  4/1/2014  15000
4     Halena  4/1/2014  12000 

the list of a single column from the dataframe
 ['1/1/2014', '2/1/2014', '3/1/2014', '4/1/2014', '4/1/2014'] 
 <class 'list'>

Use the list() Function to Convert a Dataframe Column to a List

We can also use the list() function to convert a DataFrame column to a list, by passing the DataFrame to the list() function.

We will use the same data as above to demonstrate this approach.

import pandas as pd

df=pd.DataFrame([
        ['James',   '1/1/2014',    '1000'],
        ['Michelina',   '2/1/2014',    '12000'],
        ['Marc',   '3/1/2014',    '36000'],
        ['Bob',   '4/1/2014',    '15000'],
        ['Halena',   '4/1/2014',    '12000']
        ], columns=['Name', 'DOB','Salary'])

print("Pandas DataFrame:\n\n",df,"\n")

list_of_single_column = list(df['DOB'])

print("the list of a single column from the dataframe\n",
        list_of_single_column,
        "\n",
        type(list_of_single_column))

Output:

Pandas DataFrame:

         Name       DOB Salary
0      James  1/1/2014   1000
1  Michelina  2/1/2014  12000
2       Marc  3/1/2014  36000
3        Bob  4/1/2014  15000
4     Halena  4/1/2014  12000 

the list of a single column from the dataframe
 ['1/1/2014', '2/1/2014', '3/1/2014', '4/1/2014', '4/1/2014'] 
 <class 'list'>

Related Article - Pandas Column

  • Pandas Exclude Column
  • Related Article - Pandas DataFrame

  • Convert DataFrame Column to String in Pandas
  • Convert Pandas to CSV Without Index