How To Convert the Pandas Dataframe to a Dictionary – Definitive Guide

Pandas dataframe allows you to store data as rows and columns, and dictionary allows you to store values as a key-value pair.

You can convert the pandas dataframe to a dictionary using the df.to_dict() method.

This tutorial teaches you how to convert the pandas dataframe to a dictionary using various orientations.

If You’re in Hurry…

The to_dict() method converts the dataframe into a dictionary with the dictionary orientation.

  • Column names will be dictionary keys.
  • Each key contains another dictionary with row indexes as key and cell values as dictionary key’s values.

Code

my_dict = df.to_dict()

my_dict

Output

    {'sepal_length': {0: 5.1, 1: 4.9, 2: 4.7, 3: 4.6, 4: 5.0},
     'sepal_width': {0: 3.5, 1: 3.0, 2: 3.2, 3: 3.1, 4: 3.6},
     'petal_length': {0: 1.4, 1: 1.4, 2: 1.3, 3: 1.5, 4: 1.4},
     'petal_width': {0: 0.2, 1: 0.2, 2: 0.2, 3: 0.2, 4: 0.2}}

If You Want to Understand Details, Read on…

In this tutorial, you’ll learn the different methods available to convert the pandas dataframe into a dictionary.

Sample Dataframe

This is the sample dataframe used throughout the tutorial.

This tutorial uses the iris dataset from the sklearn library.

Read How to convert sklearn datasets into pandas dataframe to learn about the sklearn datasets.

Code

import pandas as pd

from sklearn import datasets

iris = datasets.load_iris()

df = pd.DataFrame(data=iris.data)

df.columns = ["sepal_length", "sepal_width", "petal_length", "petal_width"]

df['id'] = df.index

df.set_index('id', inplace=True)

df = df.iloc[0:5]

df.head()

Dataframe Will Look Like

sepal_lengthsepal_widthpetal_lengthpetal_width
id
05.13.51.40.2
14.93.01.40.2
24.73.21.30.2
34.63.11.50.2
45.03.61.40.2

Using to_dict() method

You can use the to_dict() method to convert the pandas dataframe into a dictionary.

By default, it converts the dataframe into a dictionary with column names as keys and the value as another dictionary with row index as keys and row value of that specific column as a key value.

For example: dict like {column_name : {index1 : value1, index2 : value2}}

Code

The following code demonstrates how to convert the dataframe to a dictionary using the default orientation.

my_dict = df.to_dict()

my_dict

Output

    {'sepal_length': {0: 5.1, 1: 4.9, 2: 4.7, 3: 4.6, 4: 5.0},
     'sepal_width': {0: 3.5, 1: 3.0, 2: 3.2, 3: 3.1, 4: 3.6},
     'petal_length': {0: 1.4, 1: 1.4, 2: 1.3, 3: 1.5, 4: 1.4},
     'petal_width': {0: 0.2, 1: 0.2, 2: 0.2, 3: 0.2, 4: 0.2}}

Orientations in to_Dict()

The key-value type in the resultant dictionary can be customised using the orient parameter.

Orient=‘dict’

Use the orientation orient=‘dict’ when you want to convert the columns as keys.

For example: dict like {column_name : {index1 : value1, index2 : value2}}

my_dict = df.to_dict(orient='dict')

my_dict

Output

    {'sepal_length': {0: 5.1, 1: 4.9, 2: 4.7, 3: 4.6, 4: 5.0},
     'sepal_width': {0: 3.5, 1: 3.0, 2: 3.2, 3: 3.1, 4: 3.6},
     'petal_length': {0: 1.4, 1: 1.4, 2: 1.3, 3: 1.5, 4: 1.4},
     'petal_width': {0: 0.2, 1: 0.2, 2: 0.2, 3: 0.2, 4: 0.2}}

Orient=‘series’

Use the orientation orient=‘series’ when you want to convert the columns as key and row values as series.

For example: dict like {column_name : Series(values)}

my_dict = df.to_dict(orient='series')

my_dict

Output

    {'sepal_length': id
     0    5.1
     1    4.9
     2    4.7
     3    4.6
     4    5.0
     Name: sepal_length, dtype: float64,
     'sepal_width': id
     0    3.5
     1    3.0
     2    3.2
     3    3.1
     4    3.6
     Name: sepal_width, dtype: float64,
     'petal_length': id
     0    1.4
     1    1.4
     2    1.3
     3    1.5
     4    1.4
     Name: petal_length, dtype: float64,
     'petal_width': id
     0    0.2
     1    0.2
     2    0.2
     3    0.2
     4    0.2
     Name: petal_width, dtype: float64}

Orient=‘list’

Use the orientation orient=‘list’ when you want to convert the columns as key and row values as a list of values.

For example: dict like {column_name : list of values}

my_dict = df.to_dict(orient='list')

my_dict

Output

    {'sepal_length': [5.1, 4.9, 4.7, 4.6, 5.0],
     'sepal_width': [3.5, 3.0, 3.2, 3.1, 3.6],
     'petal_length': [1.4, 1.4, 1.3, 1.5, 1.4],
     'petal_width': [0.2, 0.2, 0.2, 0.2, 0.2]}

To learn more about the available orientations, refer to the to_dict() documentation.

Pandas dataframe to Dictionary By rows

This section teaches you to convert pandas dataframe to a dictionary by rows.

To convert the dataframe to a dictionary by rows, use the orientation orient=‘records’.

Each row is converted to one dictionary, and you’ll get a list of dictionaries as a result.

Code

df.to_dict(orient='records')

Output

    [{'sepal_length': 5.1,
      'sepal_width': 3.5,
      'petal_length': 1.4,
      'petal_width': 0.2},
     {'sepal_length': 4.9,
      'sepal_width': 3.0,
      'petal_length': 1.4,
      'petal_width': 0.2},
     {'sepal_length': 4.7,
      'sepal_width': 3.2,
      'petal_length': 1.3,
      'petal_width': 0.2},
     {'sepal_length': 4.6,
      'sepal_width': 3.1,
      'petal_length': 1.5,
      'petal_width': 0.2},
     {'sepal_length': 5.0,
      'sepal_width': 3.6,
      'petal_length': 1.4,
      'petal_width': 0.2}]

Pandas dataframe to Dictionary By Columns

This section teaches you to convert pandas dataframe to a dictionary by columns.

To convert the dataframe to a dictionary by columns, use the orientation orient=‘list’.

Each column is converted to one dictionary, and you’ll get a list of dictionaries as a result.

Code

my_dict = df.to_dict(orient='list')

my_dict

Output

    {'sepal_length': [5.1, 4.9, 4.7, 4.6, 5.0],
     'sepal_width': [3.5, 3.0, 3.2, 3.1, 3.6],
     'petal_length': [1.4, 1.4, 1.3, 1.5, 1.4],
     'petal_width': [0.2, 0.2, 0.2, 0.2, 0.2]}

Convert Pandas Dataframe to Dictionary with Index

This section teaches you how to convert pandas dataframe to a dictionary with an index as the key.

To use the index as a key, use the orientation orient=‘index’.

The dictionary will have the index as the key, and another dictionary will be created as a value.

For example: dict like {index : {column_name : value}}.

Code

my_dict = df.to_dict(orient='index')

my_dict

Output

    {0: {'sepal_length': 5.1,
      'sepal_width': 3.5,
      'petal_length': 1.4,
      'petal_width': 0.2},
     1: {'sepal_length': 4.9,
      'sepal_width': 3.0,
      'petal_length': 1.4,
      'petal_width': 0.2},
     2: {'sepal_length': 4.7,
      'sepal_width': 3.2,
      'petal_length': 1.3,
      'petal_width': 0.2},
     3: {'sepal_length': 4.6,
      'sepal_width': 3.1,
      'petal_length': 1.5,
      'petal_width': 0.2},
     4: {'sepal_length': 5.0,
      'sepal_width': 3.6,
      'petal_length': 1.4,
      'petal_width': 0.2}}

Convert Pandas Dataframe to Dictionary without index

This section teaches you how to convert pandas dataframe to a dictionary without the index value.

To exclude the index while converting the pandas dataframe to the dictionary, use the orientation orient='list'.

For example: dict like {column : [values]}.

my_dict = df.to_dict(orient='list')

my_dict

Output

    {'sepal_length': [5.1, 4.9, 4.7, 4.6, 5.0],
     'sepal_width': [3.5, 3.0, 3.2, 3.1, 3.6],
     'petal_length': [1.4, 1.4, 1.3, 1.5, 1.4],
     'petal_width': [0.2, 0.2, 0.2, 0.2, 0.2]}

Conclusion

You’ve learned how to convert the pandas dataframe to the dictionary using the to_dict() method and how to use the orientation parameters to handle the rows and columns in the dictionary.

If you have any questions, comment below.

You May Also Like

Leave a Comment