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.

Basic Example

my_dict = df.to_dict()

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

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}}

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

Sample Dataframe

This tutorial uses the iris dataset from the sklearn library and is used as a sample dataframe.

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 list of row values
  • The list of row values is another dictionary with a row index as a key

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

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

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

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

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]}

Additional Resources

Leave a Comment