Convert DataFrame to Dictionary using Pandas DataFrame to_dict() Method
Last Updated :
12 Jun, 2025
to_dict() converts a Pandas DataFrame into a dictionary. The structure of the resulting dictionary depends on the specified orientation, allowing you to choose how rows and columns are represented. Example:
Python
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3],
'B': ['x', 'y', 'z']
})
res = df.to_dict()
print(res)
Output{'A': {0: 1, 1: 2, 2: 3}, 'B': {0: 'x', 1: 'y', 2: 'z'}}
Explanation: Each column is converted into a nested dictionary where the keys are the row indices and values are the column entries.
Syntax
DataFrame.to_dict(orient='dict', into=dict)
Parameters:
- orient (str, default='dict') specifies the format of the resulting dictionary. Common options include:
orient | Description | Example |
---|
dict (default) | Dict of columns mapping to dicts of index:value pairs | {column -> {index -> value}} |
---|
list | Dict of columns mapping to lists of values | {column -> [values]} |
---|
series | Dict of columns mapping to Pandas Series | {column -> Series(values)} |
---|
split | Dict containing keys 'index', 'columns', and 'data' | {'index': [...], 'columns': [...], 'data': [...]} |
---|
records | List of dictionaries, each representing a row | [{'col1': val1, 'col2': val2}, ...] |
---|
index | Dict of index labels mapping to dicts of column:value pairs | {index -> {column -> value}} |
---|
- into (class, default=dict) is the collection type used for the resulting dictionary. By default, it is the built-in Python dict, but can be set to collections.OrderedDict or others if desired.
Returns: A dictionary (or specified mapping type) representation of the DataFrame
Examples
Example 1: In this example, we convert the DataFrame into a dictionary where each column name maps to a list of its values.
Python
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3],
'B': ['x', 'y', 'z']
})
res = df.to_dict(orient='list')
print(res)
Output{'A': [1, 2, 3], 'B': ['x', 'y', 'z']}
Example 2: In this example, we convert the DataFrame into a list of dictionaries, where each dictionary represents a row with column names as keys.
Python
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3],
'B': ['x', 'y', 'z']
})
res = df.to_dict(orient='records')
print(res)
Output[{'A': 1, 'B': 'x'}, {'A': 2, 'B': 'y'}, {'A': 3, 'B': 'z'}]
Example 3: In this example, we convert the DataFrame into a dictionary keyed by the row index, where each value is another dictionary representing the row’s data.
Python
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3],
'B': ['x', 'y', 'z']
})
res = df.to_dict(orient='index')
print(res)
Output{0: {'A': 1, 'B': 'x'}, 1: {'A': 2, 'B': 'y'}, 2: {'A': 3, 'B': 'z'}}
Example 4: In this example, the DataFrame is converted into a dictionary with separate keys for the index, columns and data.
Python
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3],
'B': ['x', 'y', 'z']
})
res = df.to_dict(orient='split')
print(res)
Output{'index': [0, 1, 2], 'columns': ['A', 'B'], 'data': [[1, 'x'], [2, 'y'], [3, 'z']]}
Similar Reads
Pandas DataFrame to_dict() Method | Convert DataFrame to Dictionary to_dict() converts a Pandas DataFrame into a dictionary. The structure of the resulting dictionary depends on the specified orientation, allowing you to choose how rows and columns are represented. Example:Pythonimport pandas as pd df = pd.DataFrame({ 'A': [1, 2, 3], 'B': ['x', 'y', 'z'] }) res = df
3 min read
How to convert Dictionary to Pandas Dataframe? Converting a dictionary into a Pandas DataFrame is simple and effective. You can easily convert a dictionary with key-value pairs into a tabular format for easy data analysis. Lets see how we can do it using various methods in Pandas.1. Using the Pandas ConstructorWe can convert a dictionary into Da
2 min read
Create pandas dataframe from lists using dictionary Pandas DataFrame is a 2-dimensional labeled data structure like any table with rows and columns. The size and values of the dataframe are mutable, i.e., can be modified. It is the most commonly used pandas object. Creating pandas data-frame from lists using dictionary can be achieved in multiple way
2 min read
How to create DataFrame from dictionary in Python-Pandas? The task of converting a dictionary into a Pandas DataFrame involves transforming a dictionary into a structured, tabular format where keys represent column names or row indexes and values represent the corresponding data.Using Default ConstructorThis is the simplest method where a dictionary is dir
3 min read
Python - Convert dict of list to Pandas dataframe In this article, we will discuss how to convert a dictionary of lists to a pandas dataframe. Method 1: Using DataFrame.from_dict() We will use the from_dict method. This method will construct DataFrame from dict of array-like or dicts. Syntax: pandas.DataFrame.from_dict(dictionary) where dictionary
2 min read
Create Pandas Dataframe from Dictionary of Dictionaries In this article, we will discuss how to create a pandas dataframe from the dictionary of dictionaries in Python. Method 1: Using DataFrame() We can create a dataframe using Pandas.DataFrame() method. Syntax: pandas.DataFrame(dictionary) where pandas are the module that supports DataFrame data struct
2 min read