How to convert a dataframe to a dictionary
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Pandas, a powerful data manipulation library in Python, is widely used for data analysis tasks. One of its features is the conversion of DataFrames to dictionaries, leveraging various approaches to fit different requirements. This article explores how to convert a DataFrame into a dictionary, discussing methods, examples, and considerations to ensure you choose the right approach for your specific use case.
Understanding a DataFrame
A DataFrame is essentially a two-dimensional, size-mutable, and potentially heterogeneous tabular data structure with labeled axes (rows and columns) in pandas. To manipulate or extract specific structures from DataFrames, it's essential to know how to convert them into other Python data structures, such as dictionaries.
Converting a DataFrame to a Dictionary
Pandas offers multiple methods to transform a DataFrame into a dictionary, primarily using the `to_dict()` function. Each method offers flexibility depending on the structure of the dictionary you require.
`to_dict()` Method
The `to_dict()` method is versatile, allowing you to specify the orientation of the dictionary with the `orient` parameter. The primary orientations include:
- `dict` (default): Creates a dictionary of lists (or arrays).
- `list`: Constructs a dictionary with the column names as keys and lists of column values as values.
- `series`: Produces a dictionary where each value is a pandas Series.
- `split`: Outputs a dictionary with three keys: `'index'`, `'columns'`, and `'data'`.
- `records`: Converts each row into a dictionary and returns a list of these dictionaries.
- `index`: Transforms each row into a dictionary with the index value as the key.
Let’s explore each with examples.
Example DataFrame
Consider a DataFrame as follows:
- Performance: Consider the size of your DataFrame and the overhead of creating multiple smaller structures when using orientations like `records`.
- Compatibility: Ensure the dictionary format fits your subsequent data operations or library requirements.
Related reading
- How to convert a NumPy array to PIL image applying matplotlib colormap
- How to convert an array of strings to an array of floats in numpy?
- How to convert index of a pandas dataframe into a column
- How to convert index of a pandas dataframe into a column
- How to convert a Java 8 Stream to an Array?
- How to convert a JSON string to a dictionary?
- How to convert a function in a third party library to be async?
- How to convert a nested Python dict to object?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.