Home » Pandas: Quickly Convert DataFrame to Dictionary

Pandas: Quickly Convert DataFrame to Dictionary

by Erma Khan

You can use the following syntax to convert a pandas DataFrame to a dictionary:

df.to_dict()

Note that to_dict() accepts the following potential arguments:

  • dict: (default) Keys are column names. Values are dictionaries of index:data pairs.
  • list: Keys are column names. Values are lists of column data.
  • series: Keys are column names. Values are Series of column data.
  • split: Keys are ‘columns’, ‘data’, and ‘index’.
  • records: Keys are column names. Values are data in cells.
  • index: Keys are index labels. Values are data in cells.

The following examples show how to use this syntax in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'B', 'B', 'C'],
                   'points': [5, 7, 9, 12, 9],
                   'rebounds': [11, 8, 6, 6, 5]})

#view DataFrame
df

	team	points	rebounds
0	A	5	11
1	A	7	8
2	B	9	6
3	B	12	6
4	C	9	5

Example 1: Convert DataFrame to Dictionary (‘dict’)

The following code shows how to convert a pandas DataFrame to a dictionary using the default ‘dict‘ method:

df.to_dict()

{'team': {0: 'A', 1: 'A', 2: 'B', 3: 'B', 4: 'C'},
 'points': {0: 5, 1: 7, 2: 9, 3: 12, 4: 9},
 'rebounds': {0: 11, 1: 8, 2: 6, 3: 6, 4: 5}}

Example 2: Convert DataFrame to Dictionary (‘list’)

The following code shows how to convert a pandas DataFrame to a dictionary using the ‘list‘ method:

df.to_dict('list')

{'team': ['A', 'A', 'B', 'B', 'C'],
 'points': [5, 7, 9, 12, 9],
 'rebounds': [11, 8, 6, 6, 5]}

Example 3: Convert DataFrame to Dictionary (‘series’)

The following code shows how to convert a pandas DataFrame to a dictionary using the ‘series‘ method:

df.to_dict('series')

{'team': 0    A
 1    A
 2    B
 3    B
 4    C
 Name: team, dtype: object,
 'points': 0     5
 1     7
 2     9
 3    12
 4     9
 Name: points, dtype: int64,
 'rebounds': 0    11
 1     8
 2     6
 3     6
 4     5
 Name: rebounds, dtype: int64}

Example 4: Convert DataFrame to Dictionary (‘split’)

The following code shows how to convert a pandas DataFrame to a dictionary using the ‘split‘ method:

df.to_dict('split')

{'index': [0, 1, 2, 3, 4],
 'columns': ['team', 'points', 'rebounds'],
 'data': [['A', 5, 11], ['A', 7, 8], ['B', 9, 6], ['B', 12, 6], ['C', 9, 5]]}

Example 5: Convert DataFrame to Dictionary (‘records’)

The following code shows how to convert a pandas DataFrame to a dictionary using the ‘records‘ method:

df.to_dict('records')

[{'team': 'A', 'points': 5, 'rebounds': 11},
 {'team': 'A', 'points': 7, 'rebounds': 8},
 {'team': 'B', 'points': 9, 'rebounds': 6},
 {'team': 'B', 'points': 12, 'rebounds': 6},
 {'team': 'C', 'points': 9, 'rebounds': 5}]

Example 6: Convert DataFrame to Dictionary (‘index’)

The following code shows how to convert a pandas DataFrame to a dictionary using the ‘index‘ method:

df.to_dict('index')

{0: {'team': 'A', 'points': 5, 'rebounds': 11},
 1: {'team': 'A', 'points': 7, 'rebounds': 8},
 2: {'team': 'B', 'points': 9, 'rebounds': 6},
 3: {'team': 'B', 'points': 12, 'rebounds': 6},
 4: {'team': 'C', 'points': 9, 'rebounds': 5}}

Additional Resources

The following tutorials explain how to perform other common data conversions in pandas:

How to Convert Pandas DataFrame to NumPy Array
How to Convert Pandas Series to NumPy Array
How to Convert Pandas DataFrame to List

Related Posts