How to Convert DataFrame to a List of Dictionaries in Python

Working with data in Python often involves using Pandas, a powerful library that provides data manipulation and analysis tools. One common task is converting a DataFrame into a list of dictionaries, which can be useful for various data processing operations. In this blog post, we will explore different approaches to convert a DataFrame to a list of dictionaries in Python.

Prerequisites:

To follow along with the examples in this blog post, you should have a basic understanding of Python and be familiar with the Pandas library. Make sure you have the necessary libraries installed, including Pandas.

Method 1: Using the to_dict() method:

The simplest way to convert a DataFrame to a list of dictionaries is by using the to_dict() method provided by Pandas. This method converts the DataFrame into a dictionary, where each column becomes a key, and the corresponding values form the associated values.

Here’s an example:

import pandas as pd

# Create a sample DataFrame
data = {'Name': ['John', 'Jane', 'Alice'],
        'Age': [25, 30, 35],
        'City': ['New York', 'London', 'Paris']}
df = pd.DataFrame(data)

# Convert DataFrame to a list of dictionaries
dict_list = df.to_dict('records')
print(dict_list)

Output:

[
    {'Name': 'John', 'Age': 25, 'City': 'New York'}, 
    {'Name': 'Jane', 'Age': 30, 'City': 'London'},
    {'Name': 'Alice', 'Age': 35, 'City': 'Paris'}
]

In the above code, we create a sample DataFrame and then use the to_dict('records') method to convert it to a list of dictionaries. The ‘records’ argument specifies that each row of the DataFrame should be represented as a dictionary.

Method 2: Iterating over DataFrame rows:

Another approach to convert a DataFrame to a list of dictionaries is by iterating over the rows and constructing dictionaries manually. This method provides more flexibility if you want to perform any additional data manipulation during the conversion process. Here’s an example:

import pandas as pd

# Create a sample DataFrame
data = {'Name': ['John', 'Jane', 'Alice'],
        'Age': [25, 30, 35],
        'City': ['New York', 'London', 'Paris']}
df = pd.DataFrame(data)

# Convert DataFrame to a list of dictionaries
dict_list = []
for _, row in df.iterrows():
    dictionary = {}
    for column in df.columns:
        dictionary[column] = row[column]
    dict_list.append(dictionary)

print(dict_list)

Output:

[
    {'Name': 'John', 'Age': 25, 'City': 'New York'}, 
    {'Name': 'Jane', 'Age': 30, 'City': 'London'},
    {'Name': 'Alice', 'Age': 35, 'City': 'Paris'}
]

In the above code, we iterate over the rows of the DataFrame using iterrows(). For each row, we create an empty dictionary, iterate over the columns, and populate the dictionary with column names as keys and corresponding row values. Finally, we append each dictionary to the dict_list.

Conclusion:

Converting a DataFrame to a list of dictionaries is a common task in Python data processing. This blog post discussed two methods: using the to_dict() method provided by Pandas and manually iterating over the DataFrame rows. The to_dict() method is more concise and straightforward, while the iteration approach provides more flexibility for customization. Choose the method that best suits your specific needs and enjoy the flexibility of workingwith data in Python using the power of Pandas.