How to convert a Pandas DataFrame to a list in Python [4 Methods]

Do you want to convert a dataframe to a list? In this Python tutorial, I will explain how to convert a Pandas dataframe to a list in Python using different methods with some illustrative examples.

To convert a Pandas DataFrame to a list in Python, various methods can be employed: the df.values.tolist() method for a direct conversion to a list of lists, the to_dict() function for creating a list of dictionaries, List Comprehension for customized list structures, and the apply() function to apply specific transformations to each row, ensuring flexibility and efficiency in the conversion process.

Convert a Pandas DataFrame to a list in Python

There are four different methods to convert a Pandas dataframe to a list in Python:

  1. df.values.tolist() method
  2. to_dict() function
  3. List Comprehension method
  4. apply() function

Let’s see them one by one using some demonstrative examples:

Note: To confirm that whatever we get as an output is a 2D list I have added a function to all of the methods: If the function returns True that means we get a 2D list in Python,

def is_2d_list(output):
    if not isinstance(output, list):
        return False
    for element in output:
        if not isinstance(element, list):
            return False
    return True

1. DataFrame to 2D list using the df.values.tolist() method

The simplest way to convert a Pandas DataFrame to a list in Python is by using the values attribute followed by the tolist() method. This method converts the DataFrame into a list of lists where each inner list represents a row in the DataFrame.

import pandas as pd

def is_2d_list(output):
    if not isinstance(output, list):
        return False
    for element in output:
        if not isinstance(element, list):
            return False
    return True

df = pd.DataFrame({
    'City': ['New York', 'Los Angeles', 'Chicago'],
    '2018': [8398748, 3990456, 2705994],
    '2019': [8336817, 3979576, 2695598]
})

list_of_lists = df.values.tolist()
print(list_of_lists)
check = is_2d_list(list_of_lists)
print(check)

Output: The function defined will confirm that the output we get is a list of lists or 2D list in Python.

[['New York', 8398748, 8336817], ['Los Angeles', 3990456, 3979576], ['Chicago', 2705994, 2695598]]
True

The output of the code executed in Pycharm is illustrated in the screenshot below.

How to convert a Pandas DataFrame to a list in Python

2. Pandas DataFrame to 2D list using the to_dict() function

If we prefer to have a list of dictionaries where each dictionary represents a row, with column names as keys, we can use the to_dict(‘records’) method to convert a Pandas DataFrame to a list in Python.

import pandas as pd

def is_2d_list(output):
    if not isinstance(output, list):
        return False
    for element in output:
        if not isinstance(element, list):
            return False
    return True
df = pd.DataFrame({
    'State': ['California', 'Texas', 'Florida'],
    'Capital': ['Sacramento', 'Austin', 'Tallahassee']
})

list_of_dicts = df.to_dict('records')
list_of_lists = [[d['State'], d['Capital']] for d in list_of_dicts]
print(list_of_lists)

check = is_2d_list(list_of_lists)
print(check)

Output:

[['California', 'Sacramento'], ['Texas', 'Austin'], ['Florida', 'Tallahassee']]
True

Below is a screenshot showing the output after the code was executed in the PyCharm editor.

df to list in Python

3. Pandas DataFrame to list of strings using list comprehension

List comprehension offers a flexible way to iterate over DataFrame rows and convert them into lists in Python. This method is highly customizable, allowing for more complex transformations during the conversion process.

import pandas as pd

def is_2d_list(output):
    if not isinstance(output, list):
        return False
    for element in output:
        if not isinstance(element, list):
            return False
    return True

df = pd.DataFrame({
    'University': ['Harvard', 'Yale', 'Princeton'],
    'Founded': [1636, 1701, 1746]
})

list_of_lists = [list(row) for row in df.values]
print(list_of_lists)

check = is_2d_list(list_of_lists)
print(check)

Output:

[['Harvard', 1636], ['Yale', 1701], ['Princeton', 1746]]
True

The following screenshot illustrates the result after running the code in the PyCharm editor.

pandas dataframe to list in Python

4. DataFrame to list using the apply() function

The apply() method applies a specified function along an axis of the DataFrame. It can be used to convert each row into a list in Python, offering a balance between simplicity and flexibility for more involved transformations.

import pandas as pd

def is_2d_list(output):
    if not isinstance(output, list):
        return False
    for element in output:
        if not isinstance(element, list):
            return False
    return True

df = pd.DataFrame({
    'State': ['California', 'Texas', 'New York'],
    'Senators': [2, 2, 2],
    'Representatives': [53, 36, 27]
})

list_of_lists = df.apply(lambda x: x.tolist(), axis=1).tolist()
print(list_of_lists)

check = is_2d_list(list_of_lists)
print(check)

Output:

[['California', 2, 53], ['Texas', 2, 36], ['New York', 2, 27]]
True

The output from the code executed in PyCharm can be seen in the screenshot provided below.

convert dataframe to list in python

Conclusion

Here, I have explained four effective methods to convert a Pandas DataFrame into a list in Python. These methods include using the df.values.tolist() method for a quick conversion to a nested list, the to_dict() function for a list of dictionaries, utilizing List Comprehension for more customized transformations, and the apply() function for applying specific formatting to each row.

READ:  Scipy Find Peaks - Useful Tutorial

Each method offers a unique approach to handling DataFrame conversion, catering to different data processing needs.

You may also like to read: