How to Convert a Pandas DataFrame to a Dict without Index in Python [3 Examples]

Do you want to convert a Pandas to dict without index? In this Pandas blog, I will tell you different methods to convert a Pandas dataframe to a dict without index in Python with the help of some examples.

To convert a Pandas DataFrame to a dictionary without including the index in Python, we can use the to_dict() function with orient=’records’ for direct conversion, employ dictionary comprehension for more customized transformations, or utilize the apply() function with a lambda to convert each row individually.

Convert a Pandas DataFrame to a Dict without Index in Python

There are three different ways to convert a Pandas dataframe to a dict without index in Python:

  1. to_dict() Function
  2. Dictionary Comprehension
  3. apply() Function

Let me explain each of the methods with the help of some illustrative examples:

1. Pandas to_dict without index using to_dict() Function

This method uses the to_dict() function of the Pandas DataFrame, specifying orient=’records’. This converts a Pandas dataframe to a list of dictionaries without an index in Python, where each dictionary corresponds to a row in the DataFrame, excluding the index.

import pandas as pd

df = pd.DataFrame({
    'City': ['New York', 'Los Angeles', 'Chicago'],
    'Population': [8419000, 3971000, 2705000]
})

dict_without_index = df.to_dict(orient='records')
print(dict_without_index)

print(type(dict_without_index))

Output:

[{'City': 'New York', 'Population': 8419000}, {'City': 'Los Angeles', 'Population': 3971000}, {'City': 'Chicago', 'Population': 2705000}]
<class 'list'>

Following the execution of the code in Pycharm, the resulting screenshot is displayed below.

How to Convert a Pandas DataFrame to a Dict without Index in Python

2. Pandas to dict without index using dictionary comprehension

Dictionary comprehension involves iterating over the rows of the DataFrame and constructing a dictionary for each row manually. This method provides more control to convert a Pandas dataframe to a dict without index in Python, allowing for custom logic or data manipulation within the comprehension loop.

import pandas as pd

df = pd.DataFrame({
    'City': ['Seattle', 'Miami', 'Austin'],
    'State': ['Washington', 'Florida', 'Texas']
})

dict_without_index = [{col: df[col][i] for col in df.columns} for i in range(len(df))]
print(dict_without_index)

print(type(dict_without_index))

Output:

[{'City': 'Seattle', 'State': 'Washington'}, {'City': 'Miami', 'State': 'Florida'}, {'City': 'Austin', 'State': 'Texas'}]
<class 'list'>

Following the execution of the code within the Pycharm editor, the subsequent screenshot displays the output.

convert dataframe to dictionary without index in Python

3. Dataframe to dict without index using apply() function

This approach employs Pandas’ apply() function, which applies a specified lambda function to each row. The lambda function converts each row to a dictionary, resulting in a series of dictionaries. This method is beneficial for applying additional transformations or computations to the data during the conversion of a Pandas dataframe to a dict without index in Python.

import pandas as pd

df = pd.DataFrame({
    'City': ['Boston', 'San Francisco', 'Denver'],
    'Area Code': [617, 415, 303]
})

dict_without_index = df.apply(lambda row: row.to_dict(), axis=1).tolist()
print(dict_without_index)

print(type(dict_without_index))

Output:

[{'City': 'Boston', 'Area Code': 617}, {'City': 'San Francisco', 'Area Code': 415}, {'City': 'Denver', 'Area Code': 303}]
<class 'list'>

The screenshot provided below illustrates the results post-execution of the code in the Pycharm editor.

dataframe to dictionary without index in Python

Conclusion

Here, I have explained three effective methods to convert a Pandas DataFrame to a dictionary without the index in Python: using the to_dict() function with orient=’records’, leveraging dictionary comprehension for more control, and applying the apply() function for row-wise transformation. Each method offers a different approach to efficiently transform DataFrame data into a dictionary format, catering to various data processing needs.

You may also like to read: