Flexiple Logo
  1. Home
  2. Blogs
  3. Python
  4. Dataframe to List in Python

Dataframe to List in Python

Author image

Harsh Pandey

Software Developer

Published on Thu Mar 28 2024

Introduction

In Python, a DataFrame is a two-dimensional tabular data structure provided by libraries like Pandas. It offers powerful tools to manipulate and analyze data efficiently. There are scenarios where we might need to convert a DataFrame into a list to work with the data in a different format or to perform specific operations. In this blog, we'll explore various methods to convert a DataFrame to a list in Python.

Converting Python Dataframe to List using ‘values’ property and Dataframe constructor

In this method, we use the values property of a DataFrame to convert it to a NumPy array. Then, we convert the NumPy array to a Python list. The values property returns the underlying data as a two-dimensional NumPy array, where each row corresponds to a row in the DataFrame, and each column represents a column in the DataFrame.

Code Example:

import pandas as pd

# Sample DataFrame
data = {'Name': ['John', 'Alice', 'Bob'],
        'Age': [28, 24, 22]}
df = pd.DataFrame(data)

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

The data_list will be:

[['John', 28], ['Alice', 24], ['Bob', 22]]

Converting Python Dataframe to List using to_dict method

In this method, we first convert the DataFrame into a dictionary using the to_dict method, specifying the orient parameter as 'records'. The 'records' orient returns a list of dictionaries where each dictionary corresponds to a row in the DataFrame.

Code Example:

import pandas as pd

# Sample DataFrame
data = {'Name': ['John', 'Alice', 'Bob'],
        'Age': [28, 24, 22]}
df = pd.DataFrame(data)

# Converting DataFrame to a dictionary
data_dict = df.to_dict(orient='records')

# Converting dictionary to a list
data_list = list(data_dict)

The resulting data_list will be:

[{'Name': 'John', 'Age': 28}, {'Name': 'Alice', 'Age': 24}, {'Name': 'Bob', 'Age': 22}]

Converting Python Dataframe to List using iterrows method

In this method, we use the iterrows method of the DataFrame to iterate over its rows one by one. For each row, we convert it to a Python list and append it to the final list containing all rows' data.

Code Example:

import pandas as pd

# Sample DataFrame
data = {'Name': ['John', 'Alice', 'Bob'],
        'Age': [28, 24, 22]}
df = pd.DataFrame(data)

# Converting DataFrame to a list using iterrows
data_list = [list(row) for index, row in df.iterrows()]

The resulting data_list will be:

[['John', 28], ['Alice', 24], ['Bob', 22]]

Conclusion

In this blog, we discussed three different methods to convert a DataFrame to a list in Python. Each method has its advantages and may be more suitable depending on your specific use case and the size of your dataset. The values property and the to_dict method are more straightforward and efficient, while the iterrows method offers more control over the conversion process. Choose the method that best fits your needs and enables you to manipulate and work with the DataFrame data effectively in your Python projects.

Related Blogs

Browse Flexiple's talent pool

Explore our network of top tech talent. Find the perfect match for your dream team.