How can I efficiently convert a Pandas DataFrame to a dictionary?

How can I efficiently convert a Pandas DataFrame to a dictionary?

Converting a Pandas DataFrame to a dictionary can be done efficiently by using the built-in function “to_dict()” in Pandas. This function converts the data in the DataFrame into a dictionary format, with the column names as keys and the corresponding values in each row as the values. This method is efficient as it eliminates the need for manual iteration and conversion, saving time and effort. Additionally, the “orient” parameter in the “to_dict()” function allows for customization of the dictionary structure, making it suitable for different use cases. Overall, using the “to_dict()” function is a quick and efficient way to convert a Pandas DataFrame to a dictionary.

Pandas: Quickly Convert DataFrame to Dictionary


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

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

How to Convert Pandas DataFrame to List

Cite this article

stats writer (2024). How can I efficiently convert a Pandas DataFrame to a dictionary?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-efficiently-convert-a-pandas-dataframe-to-a-dictionary/

stats writer. "How can I efficiently convert a Pandas DataFrame to a dictionary?." PSYCHOLOGICAL SCALES, 3 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-efficiently-convert-a-pandas-dataframe-to-a-dictionary/.

stats writer. "How can I efficiently convert a Pandas DataFrame to a dictionary?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-efficiently-convert-a-pandas-dataframe-to-a-dictionary/.

stats writer (2024) 'How can I efficiently convert a Pandas DataFrame to a dictionary?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-efficiently-convert-a-pandas-dataframe-to-a-dictionary/.

[1] stats writer, "How can I efficiently convert a Pandas DataFrame to a dictionary?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I efficiently convert a Pandas DataFrame to a dictionary?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
PDF
Scroll to Top