How to Convert Pandas DataFrame into a List?


Converting a Pandas DataFrame into a list is a common task in data analysis and manipulation using Python. The Pandas library provides powerful data structures and functionalities for handling tabular data, but there are situations where it becomes necessary to transform the DataFrame into a list format. By converting a DataFrame into a list, we gain flexibility in performing various operations or working with other Python data structures.

In this article, we will explore different methods to convert a Pandas DataFrame into a list. We will discuss simple approaches like using the values attribute or the to_dict() method, as well as employing list comprehension. By providing detailed steps and examples, this article will equip you with the knowledge to convert a DataFrame into a list.

Method 1: Using the values attribute

One of the truthful approaches to converting a Pandas DataFrame into a list involves utilizing the values attribute. By accessing the values attribute, we retrieve the underlying Numpy array representation of the DataFrame. Subsequently, we can transform this array into a list using the tolist() method. This method provides a convenient way to obtain a list representation of the DataFrame, facilitating further data manipulation and analysis.

Example

Here's an input example:

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'Column1': [1, 2, 3], 'Column2': ['A', 'B', 'C']})

# Convert DataFrame to a list using the 'values' attribute
df_list = df.values.tolist()

# Output the converted list
print(df_list)

Output

[[1, 'A'], [2, 'B'], [3, 'C']]

Method 2: Using the to_dict() method

Pandas offers a convenient method called to_dict() that allows us to convert a DataFrame into a dictionary. By default, this method transforms the DataFrame into a dictionary where the column names serve as keys mapped to their respective values. However, to obtain a list representation of the DataFrame, we can retrieve the dictionary values by using the values() method.

Example

Here's an input example:

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'Column1': [1, 2, 3], 'Column2': ['A', 'B', 'C']})

# Convert DataFrame to a dictionary
df_dict = df.to_dict()

# Convert the dictionary values to a list
df_list = list(df_dict.values())

# Output the converted list
print(df_list)

Output

[{0: 1, 1: 2, 2: 3}, {0: 'A', 1: 'B', 2: 'C'}]

Method 3: Using list comprehension

List comprehension is another approach for converting a Pandas DataFrame into a list. It allows us to iterate over the rows or columns of the DataFrame and effortlessly append the values to a new list. This concise and efficient method simplifies the extraction of data from the DataFrame, enabling us to construct a list representation with ease. List comprehension offers flexibility, allowing customization of the conversion process to meet specific requirements, making it a versatile technique for DataFrame−to−list conversion.

Example

Here's an input example:

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'Column1': [1, 2, 3], 'Column2': ['A', 'B', 'C']})

# Convert DataFrame to a list using list comprehension
df_list = [list(row) for row in df.values]

# Output the converted list
print(df_list)

Output

[[1, 'A'], [2, 'B'], [3, 'C']]

Conclusion

In summary, converting a Pandas DataFrame into a list enhances your data analysis and capabilities in Python. We explored various methods, such as using the values attribute to extract the underlying Numpy array and converting it to a list with tolist(). The to_dict() method converts the DataFrame into a dictionary, which can then be transformed into a list. List comprehension provides a concise and efficient approach, iterating over rows or columns to construct a list representation. Converting a DataFrame into a list opens possibilities for analysis, manipulation, and integration with other data structures. Mastering these techniques empowers you to process data efficiently in Python.

Updated on: 21-Jul-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements