How to convert Python Dataframe to List (11 Easy Methods)

Python, renowned for its simplicity and versatility, provides a powerful data manipulation tool known as Pandas. Among its many functionalities, Pandas introduces the concept of DataFrames – a two-dimensional, labeled data structure that opens up a plethora of possibilities for data analysis and manipulation. In this article, we will delve into the art of converting Python DataFrames to lists, exploring the various methods available.

Understanding Python DataFrames

Before we dive into the conversion process, it’s crucial to understand what Python DataFrames are. A DataFrame is essentially a tabular data structure comprised of rows and columns. It allows for easy handling and manipulation of data, making it a go-to choice for data scientists and analysts.

Conversion Methods

1. Using values Attribute:

The simplest method involves using the values attribute of the DataFrame. This attribute returns a NumPy array, which can then be easily converted to a list using the tolist() method.

import pandas as pd

# Creating a sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 22],
'City': ['New York', 'San Francisco', 'Los Angeles']}

df = pd.DataFrame(data)

# Converting DataFrame to list
df_list = df.values.tolist()

2. Using to_list() Method:

Pandas provides a dedicated method, to_list(), for converting specific columns of DataFrame to lists. This method allows for selecting individual columns or converting the entire DataFrame.

# Converting specific column to list
age_list = df['Age'].to_list()

# Converting entire DataFrame to list of lists
df_list = df.to_numpy().tolist()

The to_numpy() method is used here to convert the DataFrame to a NumPy array before calling tolist().

3. Using List Comprehension:

For more flexibility and customization, list comprehension can be employed to iterate through the DataFrame and construct lists.

# Converting specific column to list using list comprehension
age_list = [age for age in df['Age']]

# Converting entire DataFrame to list of lists using list comprehension
df_list = [[value for value in row] for row in df.values]

This method provides control over which elements to include in the lists.

4. Using iterrows() Method:

The iterrows() method allows iteration through the DataFrame rows as (index, Series) pairs, making it suitable for creating lists.

# Converting DataFrame to list of lists using iterrows()
df_list = [list(row[1]) for row in df.iterrows()]

This method is particularly useful when you need both the index and values in your list.

5. Using apply() Method:

The apply() method can be employed to apply a function along the axis of the DataFrame, enabling custom transformations.

# Converting DataFrame to list using apply() for a specific function
name_list = df['Name'].apply(lambda x: x.upper()).tolist()

This example converts the ‘Name’ column to uppercase before converting it to a list.

6. Using numpy.ndarray.flatten() Method:

If you have a multi-dimensional DataFrame and want a flat list, you can use numpy.ndarray.flatten() after converting the DataFrame to a NumPy array.

import numpy as np
# Converting multi-dimensional DataFrame to a flat list
flat_list = df.to_numpy().flatten().tolist()


This method is handy for certain scenarios where a flattened structure is required.

7. Using stack() Method:

The stack() method can be used to pivot the DataFrame, converting it into a multi-level Series, which can then be converted to a list.

# Converting DataFrame to list using stack()
stacked_list = df.stack().tolist()

This is effective when you want to handle multi-level indices.

8. Using json Module:

The to_json() method can be utilized to convert the DataFrame to a JSON string, and the json.loads() function can then be used to load it as a list.

import json

# Converting DataFrame to list using json module
df_list = json.loads(df.to_json(orient='records'))

This method is useful when interoperability with other systems that understand JSON is required.

9. Using numpy.ndarray.tolist() for Specific Columns:

If you only want to convert specific columns, you can use the tolist() method directly on the NumPy array of those columns.

# Converting specific columns to lists using tolist() on NumPy array
selected_columns = ['Name', 'City']
selected_lists = df[selected_columns].to_numpy().tolist()

This provides a targeted approach to list conversion for selected columns.

10. Combining Lists from Different Columns:

If you want to create a list that combines elements from multiple columns, you can use list comprehension for a customized merging strategy.

# Combining elements from 'Name' and 'City' columns into a single list
combined_list = [f"{name} - {city}" for name, city in zip(df['Name'], df['City'])]

This example creates a list with elements formatted as “Name – City”.

11. Using tolist() with DataFrame Slicing:

DataFrame slicing provides an efficient way to select specific rows and columns. By combining slicing with the tolist() method, you can create a list tailored to your needs.

# Selecting specific rows and columns and converting to a list
selected_rows = df.loc[1:2, ['Name', 'City']].values.tolist()

In this example, we select rows 1 to 2 and only the ‘Name’ and ‘City’ columns, creating a list of lists containing the specified data.

This approach is beneficial when you need a subset of the DataFrame, allowing for a more focused extraction of data into a list.

Conclusion

Converting Python DataFrame to list is a fundamental skill for data manipulation and analysis. In this article, we explored various methods, ranging from straightforward attributes like values to more versatile approaches like list comprehension. Depending on the specific requirements of your project, you can choose the method that best suits your needs. Armed with this knowledge, you are now better equipped to harness the full potential of Python DataFrames in your data-related endeavors. Thank you.

Leave a Comment

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